How to draw a line in VB.NET

I am trying to make a simple string with VB.NET.

My code is as below, however, when I run the code, only the form is displayed! There is no line.

What have I done wrong here?

Public Class Form1 Dim pen As System.Drawing.Graphics Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load pen = Me.CreateGraphics() pen.DrawLine(Pens.Azure, 10, 10, 20, 20) End Sub End Class 
+4
source share
4 answers

Basically, you were mistaken in using the CreateGraphics method.

This is something you rarely, if ever, need to do. It is not as if the method was violated, of course. It does exactly what it says: it is documented as performing: it returns a Graphics object representing the surface of the drawing of your form.

The problem is that whenever your form is redrawn (which can happen for a variety of reasons), the Graphics object basically gets reset. As a result, everything that you painted in what you received is erased.

The form is always redrawn on first load, so using CreateGraphics never makes sense in the Load event handler method. It will also be redrawn at any time when it is minimized and restored, closed by another window or even resized (some of them depend on your operating system, graphics drivers, and the properties of your form, but outside the dot) sub>.

The only time you can use CreateGraphics is when you want to show immediate user feedback that should not be saved when redrawing. For example, in the MouseMove event handler when displaying feedback for drag and drop.

So what is the solution? Always execute the drawing inside the Paint event handler method. This way, it is saved during the redraw because the redraw basically involves creating a Paint event.

When the Paint event is raised, an instance of the PaintEventArgs class is PaintEventArgs , which contains a Graphics object that you can draw.

So what your code looks like:

 Public Class Form1 Protected Overridable Sub OnPaint(e As PaintEventArgs) ' Call the base class MyBase.OnPaint(e) ' Do your painting e.Graphics.DrawLine(Pens.Azure, 10, 10, 20, 20) End Sub End Class 

(Note also that in the above code, I override the OnPaint method rather than handling the corresponding Paint event. This is considered the best practice for handling events in a derived class. But it will work anyway.)

+9
source

You must do this to draw your line.

 Public Class Form1 Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint Dim myPen As Pen 'instantiate a new pen object using the color structure myPen = New Pen(Color=Color.Blue, Width=2) 'draw the line on the form using the pen object e.Graphics.DrawLine(pen=myPen, x1=100, y1=150, x2=150, y2=100) End Sub End Class 

or there is a simpler solution, just add this code to the Paint Paint event

 e.Graphics.DrawLine(Pens.Azure, 10, 10, 20, 20) 
+2
source

You have to put this code in the Paint event of the form, which happens here when the line is drawn, but the form is redrawn when loading finishes, so your line disappears. Also, try black or a more contrasting color, or you can skip it against the background color of the form window.

+1
source

You can achieve this by adding a group control to the form. Then delete the text (keep the blank text), set the height to 1 and select the desired BackColor.

0
source

Source: https://habr.com/ru/post/1493788/


All Articles