C# How do I draw a line between two objects on a windows form?

ExoSkull

I've been trying to draw a line between two objects for a long time now, but it still won't work.

My program is supposed to make two picture boxes (Already made, called PB1 and PB2) and connect them with a line on the form.

I have this:

public void DrawStuff(object sender, PaintEventArgs e)
    {
        Pen blackPen = new Pen(Color.Black, 3);
        Point point1 = new Point(PB[0].Location.X, PB[0].Location.Y);
        Point point2 = new Point(PB[1].Location.X, PB[1].Location.Y);

        e.Graphics.DrawLine(blackPen, point1, point2);
        CreateGraphics();
    }

But I can't call the function! Also, the Boxes are being created with a button, so it can't draw from the start, it has to do it after I push that button. If anyone has a working code, please let me know, I'm about to break down.

dotNET
  1. Do not (read NEVER EVER) call CreateGraphics() explicitly. This is a crime against humanity, except for very rare situations.
  2. Handle Paint event (or override OnPaint()) of your Form. Write your line drawing code in there.

Something like this:

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

  using(var blackPen = new Pen(Color.Black, 3))
    e.Graphics.DrawLine(blackPen, PB[0].Location, PB[1].Location);
}
  1. Whenever you need to refresh screen manually, call this.Invalidate().

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I draw an Ideal line between two headers?

How do I draw a horizontal line between two circles with CSS?

How do I draw a line on a Lazarus form?

How to draw a line between two points objective-C

How do I draw the plot of a function between two horizontal boundaries?

VB 13 windows form - how do I pass my variable between two forms [New to coding]

How do I get the time difference between two DateTime objects using C#?

How do I differentiate between two json objects with same key?

How do i calculate the length of a line between two points?

How to draw vertical separation line between two text line android?

How can I draw a line between two google map native markers? (Ionic 3, cordova)

Qt, C++ Draw Connector line between 2 Objects

How do I draw a line in Forth with OpenGL?

Basics of LiveCharts -- How do I draw a Line?

How do I draw an angled Line

How do I run two commands in one line in Windows CMD?

How do I make pygame draw along all points on a line between 2 points?

How can i draw Two line with some angle between them, later can change angle by dragging any line

How to draw image dynamically on a canvas line between two cells

How to draw a line between two point in Google map?

Matplotlib how to draw vertical line between two Y points

How to draw line between two circle in html canvas element

How to draw a line between two points over an image in swift 3?

How to draw a line between two views in Swift 3?

How to draw a line between two given points in R with plotly?

How to re-draw line connected between moveable two UIView

How to draw a line between two points with js and CSS

How do I draw an arrow between two points in d3v4?

How do I draw a route, along an existing road, between two points?