Hello every body
I 'm new in vb.net I tried to write a little app drawing a line into a picture box and
show it
here the code
Public Class Form1
Private Sub draws()
Dim mypen As Pen
Dim gr As System.Drawing.Graphics
gr = panel2.CreateGraphics
mypen = New Pen(Color.Chocolate, 3)
gr.DrawLine(mypen, 20, 20, 700, 700)
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
DisplayPanel()
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
draws()
End Sub
Private Sub DisplayPanel()
Me.Panel2.BackgroundImage = Image.FromFile("E:\drawnet\Draw\Draw\bin\Debug\lcws\board.bmp")
Me.Panel2.Width = Me.Panel2.BackgroundImage.Width
Me.Panel2.Height = Me.Panel2.BackgroundImage.Height
Me.Panel2.Left = Me.Width / 2 - Me.Panel2.Width / 2
Me.Panel2.Top = Me.Height / 2 - Me.Panel2.Height / 2
End Sub
End Class
panel2 is the name of the picture box.
When i run the app i didn't see the line but only if i create an event like mouse click etc. why?
thanks for explanation
Loading
Hirendra SisodiyaPosted Apr 22, 2010, 2:18 AM
Hello Shay
you are doing this in wrong way ifyou are using paint event you should use its paint eventargs for initializing graphics 'gr' object
see this
if you want todraw a line on form, use its paint event like this:
Private Sub Form2_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim gr As System.Drawing.Graphics
gr = e.Graphics
Dim mypen As Pen
mypen = New Pen(Color.Chocolate, 3)
gr.DrawLine(mypen, 20, 20, 700, 700)
End Sub
or if you drawa line on picture box than uselike its paint event:
Private Sub Panel2_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel2.Paint
Dim gr As System.Drawing.Graphics
gr = e.Graphics
Dim mypen As Pen
mypen = New Pen(Color.Chocolate, 3)
gr.DrawLine(mypen, 20, 20, 700, 700)
End Sub
thanks
Please mark as answere if it helps
Shay WilnerPosted Apr 22, 2010, 9:58 AM
It's work !!!