How to trigger the Drawing process of a Picturebox by a button?

  • Thread starter Thread starter jasonfan2000
  • Start date Start date
J

jasonfan2000

Guest
Hi,

The great .net master tommytwotrain here has told me to use the paint event to handle the drawing. But the example he had shown me is too advanced for me to understand at this moment.

So I open another post to ask. Because the original posts have already marked as an answer and I don't want to make them too long to avoid confusing other people.

I've written some simple codes to test it myself. He has mentioned to me that in order to make the drawing persisting within the Picturebox, I should do it by using the paint event. I found it is definitely true but have no idea how to do if I want to trigger the drawing process by a button instead.

Here's the code:

This is an example to draw by normal way. It works perfect and the drawing is persistent no matter you resize the form or cover by another form.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Public Class Form1
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
Dim rect1 As New Rectangle(20, 10, 100, 100)
Dim rect2 As New Rectangle(120, 10, 100, 100)
Dim rect3 As New Rectangle(20, 110, 100, 100)
Dim rect4 As New Rectangle(120, 110, 100, 100)

Dim Brush1 As SolidBrush = New SolidBrush(Color.Beige)
Dim Brush2 As SolidBrush = New SolidBrush(Color.Blue)
Dim Brush3 As SolidBrush = New SolidBrush(Color.Red)
Dim Brush4 As SolidBrush = New SolidBrush(Color.Yellow)

e.Graphics.FillRectangle(Brush1, rect1)
e.Graphics.FillRectangle(Brush2, rect2)
e.Graphics.FillRectangle(Brush3, rect3)
e.Graphics.FillRectangle(Brush4, rect4)
End Sub

++++++++++++++++++++++++++++++++++++++++++++

But, the problem is that if I do not want the program to draw at the beginning. I want to trigger the process by a button or other event instead. If I change the code like this:

+++++++++++++++++++++++++++++++++++++++++++++

Imports System.Drawing.Drawing2D
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim MyGraphics As Graphics = PictureBox1.CreateGraphics
Dim rect1 As New Rectangle(20, 10, 100, 100)
Dim rect2 As New Rectangle(120, 10, 100, 100)
Dim rect3 As New Rectangle(20, 110, 100, 100)
Dim rect4 As New Rectangle(120, 110, 100, 100)

Dim Brush1 As SolidBrush = New SolidBrush(Color.Beige)
Dim Brush2 As SolidBrush = New SolidBrush(Color.Blue)
Dim Brush3 As SolidBrush = New SolidBrush(Color.Red)
Dim Brush4 As SolidBrush = New SolidBrush(Color.Yellow)

MyGraphics.FillRectangle(Brush1, rect1)
MyGraphics.FillRectangle(Brush2, rect2)
MyGraphics.FillRectangle(Brush3, rect3)
MyGraphics.FillRectangle(Brush4, rect4)
End Sub
End Class
++++++++++++++++++++++++++++++++++++++++++++++

It is triggered by the click button. But the drawing is not persistent because it will gone if I resize the form.

I think it should be something like calling the paint event within the button click event. But I just don't know what value to pass in.

Can anyone tell me how to handle this? Or, if Tommy himself has seen this post and if it is not too bother him, please kindly help again.

BTW, I am not a student. Not even in the IT field. Currently I just want to learn .net for my own interest. So, please feel free to shown me the sample code if you have. And don't worry about the lecture assignment or cheating things. It does not apply to me. I am far to old to be a student:) I just want to learn the .net GDI thing myself.

Thanks.

Jason

Continue reading...
 
Back
Top