Home > Back-end >  C# Windows Forms Link two buttons by line
C# Windows Forms Link two buttons by line

Time:01-02

Hay, I have two buttons (image 1), and i would like to link this buttons by line. I thought about drawing line by Class Graphics and Pen, but I tried this and it doesn't work.

image1

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            var lineBegin = new Point(button1.Left   button1.Width - 1, button1.Top   button1.Height / 2);
            var lineEnd = new Point(button2.Left, button2.Top   button2.Height / 2);

            e.Graphics.DrawLine(Pens.Maroon, lineBegin, lineEnd);
        }

CodePudding user response:

This works

pic

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        e.Graphics.DrawLine(
            Pens.Blue,
            button1.Right   2,
            button1.Top   button1.Height / 2,
            button2.Left - 2,
            button2.Top   button2.Height / 2);
    }
}
  • Related