Question

Locked

GDI+ Persistency

By lomsadef ·
I am trying to draw a shapes on one of the control on the form (like panel or picturebox ) by mouse clicks. I am wanting to drawing lines as in the Microsoft Paint; mouse down for starting point, mouse up for end point. I tried to do very simple program as (not by points, by stating their coordinates in public variables):

Imports System.Drawing
Imports System.IO

Public Class Form1


Public x1, y1, x2, y2 As Integer
Public drawclick As Boolean

Protected Overrides Sub OnPaint(ByVal pe As System.Windows.Forms.PaintEventArgs)

Dim grap As Graphics = pe.Graphics
MyBase.OnPaint(pe)
' I also add the drawing code here! But no success
If drawclick = True Then
grap.DrawLine(Pens.Blue, x1, y1, x2, y2)
End If

End Sub

Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown

x1 = e.X
y1 = e.Y

End Sub

Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp

x2 = e.X
y2 = e.Y
drawclick = True

End Sub

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

If drawclick = True Then
Dim grap As Graphics = e.Graphics
grap.DrawLine(Pens.Blue, x1, y1, x2, y2)
End If

End Sub
End Class

and when i debug the program i noticed that, although program successfully enter the code inside the paint event of picturebox control, no line was drawn. I could not understand the reason, i controlled the variables x1,y1,x2 and y2 inside the paint event, they are correctly transferred but no drawing can be seen? In addition, when i minimize and maximize the form, only the last drawn line drawn, not lines drawn before. Can anyone help me about this situation? Where is the problem? I read some forums about onpaint event, but i do not understand the "mybase.onpaint", i only want to draw a line on the picturebox control, in object browser, onpaint event for picturebox stated as "ByVal pe As System.Windows.Forms.PaintEventArgs", different than "e". What is the difference i do not understand? Can VB understand that, "pe" for picturebox? If yes, how? I am a beginner to VB, and i use VB 2005, thanks for any help.

This conversation is currently closed to new comments.

5 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

.. <N/T>

by OldER Mycroft In reply to GDI+ Persistency
Collapse -

Not N/T now, since editing finished

by lomsadef In reply to .. <N/T>

I was editing my question, since one of my code part was missing and i did not want to confuse people's mind by my wrong code part and start an misguided discussion. My question is there now with right code.

Collapse -

I had also tried bitmap copy method

by lomsadef In reply to .. <N/T>

I want to state that i also tried to apply copying my drawing to first on the bitmap and then to picturebox's image property but i got the same results.

Collapse -

I found the solution! Thanks.

by lomsadef In reply to GDI+ Persistency

I have found the solution myself, thanks.

Back to Community Forum
5 total posts (Page 1 of 1)  

Related Discussions

Related Forums