T
T
theskrutter2021-03-17 21:22:10
C++ / C#
theskrutter, 2021-03-17 21:22:10

How to display a line in Winforms C#?

Good day, I'm creating a project, and I stumbled upon a problem in the need to draw a line.
After studying the documentation for Windows Forms, I realized that I need to create a custom element.
But after I created it, nothing works, help me figure out what the problem is
Actually, here is the code:

using System.Drawing;
using System.Windows.Forms;

namespace ProjectsTests
{
    public class Line : Control // Line control sample
    {
        public Line() // Line constructor
        {
            InitializeComponent(); // Initialize Component
        }

        public Color col = Color.White; // Line color
        public Point FirstPos = new Point(10, 45); // Line first position
        public Point SecondPos = new Point(30, 45); // line second position
        public float width = 3; // line width
       
        public void Connector_Paint(object sender, PaintEventArgs e)
        {
            // Draw simple line
            Graphics g = e.Graphics;
            g.DrawLine(new Pen(col, width), FirstPos, SecondPos);
        }

        private void InitializeComponent() // Initialize Component method
        {
            this.SuspendLayout();
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.Connector_Paint);
            this.ResumeLayout(false);
        }
    }
}


I do everything in Visual Studio. Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BasiC2k, 2021-03-17
@theskrutter

Why such difficulties? Take a Panel, set a visible Border, and give it a minimum height.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question