Question Draw on a control on form load

simone.rosina

New member
Joined
Sep 10, 2010
Messages
2
Programming Experience
5-10
Dear all, I'm new in the forum. I have a simple but annoying problem: in a ordinary form I added a label (Label1) and a button (Button1) with the followiing handler.
Well if I press Button1 my dot dashed line is correctly drawn over Label1, but what I need is that program automatically does it on form load.

Please help me, thanks in advance.

Simone

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Graphic As System.Drawing.Graphics
Dim Pen As System.Drawing.Pen

Pen = New System.Drawing.Pen(System.Drawing.Color.Cyan, 2)
Pen.DashStyle = Drawing2D.DashStyle.DashDot

Graphic = Label1.CreateGraphics
Graphic.DrawLine(Pen, 0, CInt(Label1.Height / 2) - 2, CInt(Label1.Width), CInt(Label1.Height / 2) - 2)
Graphic.Dispose()
Pen.Dispose()
End Sub
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,347
Location
Norway
Programming Experience
10+
Use the Paint event of the Label, use e.Graphics to draw with (this Graphics instance you don't Dispose). For example:
Code:
Private Sub Label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Label1.Paint
    Dim lbl As Label = CType(sender, Label)
    Using dotpen As New System.Drawing.Pen(System.Drawing.Color.Cyan, 2)
        dotpen.DashStyle = Drawing2D.DashStyle.DashDot
        Dim y As Integer = CInt(lbl.Height / 2) - 2
        e.Graphics.DrawLine(dotpen, 0, y, lbl.Width, y)
    End Using
End Sub
 
Top Bottom