Answer the question
In order to leave comments, you need to log in
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);
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question