Form1 Load Event

Barabas

New member
Joined
Mar 3, 2010
Messages
4
Programming Experience
10+
I am trying to develope a small graphics application in VB.NET. My startup form has a picture box with a background image loaded from a file. The user can click spots on the picture box and this draws some circles on it. On exit the coordinates for these spots are saved to a file.

In my form load procedure I load the background image, the coordinates for the graphics(saved from the last time the program was run), and then call the drawing procedure.

The drawings appear for an instant and then dissappear.

If you click the picture box the graphics re-appear.
The form works fine once you do this.


I guess the picture box is refreshed by the form load event and this is causing the drawings to be wiped.(I stalled the call to the drawing procedure by using a timer and this works but I am sure there is a proper way of fixing this)

Is there a way of ensuring a form is fully loaded or making sure the drawing procedure occurs only when the form is fully loaded?

HERES THE CODE:

Private Sub Form2_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load

Me.WindowState = FormWindowState.Maximized
Me.Show()
Application.DoEvents()
picturebox1.BackgroundImage = System.Drawing.Image.FromFile ("C:\test.bmp")
loadspots() ' Coordinates for the drawings
DrawBoxes()
End Sub

Private Sub DrawBoxes()
Me.Refresh()
Dim m As Short
PictureBox1.Refresh()

Dim Dg As System.Drawing.Graphics
Dim FT As New Font("Arial", 10, FontStyle.Bold)

Dg = PictureBox1.CreateGraphics
For m = 0 To 5
If LbY(m).Text <> "" Then
Dg.FillEllipse(Brushes.Red, CInt(lbX(m).Text) - 10, CInt(LbY(m).Text) - 10, 20, 20)
Dg.DrawString(Str(m + 1), FT, Brushes.Black, CInt(lbX(m).Text) - 8, CInt(LbY(m).Text) - 8)
Dg.FillEllipse(Brushes.Red, CInt(lbX(m).Text) - WhiteOff - 10, CInt(LbY(m).Text) - 10, 20, 20)
End If
Next m
Dg.Dispose()
End Sub


Any Help would be appreciated.
 
Thanks jmcilhinney

Works a dream. I put your code into a project and simplified it to do just what I want to do. (Interesting Code example. Includes some useful stuff that I will no doubt need to refere to in the future)

This is an example of what I am trying to do:

Public Class Form1
Dim pic2 As Bitmap
Dim XP As Integer
Dim YP As Integer

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

Private Sub DrawLines(ByVal g As Graphics)
g.FillEllipse(Brushes.Red, XP, YP, 20, 20)
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.WindowState = FormWindowState.Maximized
Me.Show()
Application.DoEvents()
PictureBox1.Left = 0
PictureBox1.Top = 0
PictureBox1.Width = Screen.PrimaryScreen.Bounds.Width
PictureBox1.Height = Screen.PrimaryScreen.Bounds.Width
XP = 10
YP = 10
pic2 = System.Drawing.Image.FromFile("C:\SShot.bmp")
PictureBox1.BackgroundImage = pic2
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
XP = XP + 10
YP = YP + 10
PictureBox1.Refresh()
End Sub

End Class


Pressing Button1 redraws the circle in a new position each time it is pressed.

ONE FINAL QUESTION:

How do I clear the graphics from the PictureBox altogether leaving just the background image? I could set the values for XP and YP very small but I'm sure there is a proper way of doing it.

Thanks again for your help.

This is my first attempt at VB.NET having been a loyal fan of VB6 for years so forgive me if the solution is obvious.
 
ONE FINAL QUESTION:

How do I clear the graphics from the PictureBox altogether leaving just the background image? I could set the values for XP and YP very small but I'm sure there is a proper way of doing it.
PictureBox.Image = Nothing
 
ONE FINAL QUESTION:

How do I clear the graphics from the PictureBox altogether leaving just the background image? I could set the values for XP and YP very small but I'm sure there is a proper way of doing it.

You'll find that the answer to that is in my code already. What do we do when we want to change the drawing? We edit the collection and then refresh the control to draw the entire contents of the collection. If you want to draw nothing at at, what does that mean you have to do to the collection? Refer back to the code and you'll see I already handle that situation.
 
What a thoroughly pompous and arrogant way to address a person. Even decided to use the Royal "WE".

Just because you helped with a small problem it does not entitle you to speak to me like a spoiled schoolboy. Work on your interpersonal skills a bit!!!

I showed you courtesy and respect and I expect the same from you.
 
I showed you courtesy and respect and I expect the same from you.

Showing courtesy and respect does not mean you can expect the same :) Especially when, as jmcilhinney says, the EXACT answer to your SPECIFIC question was in the link he provided! In case you missed it, I looked it up for you:-

VB.NET:
Private Sub Clear()
    'Clear all unsaved lines.
    Me.lines.Clear()
    'Force the control to repaint so the lines are removed.
    Me.PictureBox1.Refresh()
End Sub

That's pretty self-explanatory to me. Had you been a complete noob to programming, jmcilhinney *might* have been a little more understanding, but as you pointed out that you have been a VB6 fan for years, I think he is perfectly entitled to expect you to read, digest and learn, rather than just pick out what you thought was relevant :)
 
Showing courtesy and respect does not mean you can expect the same :) Especially when, as jmcilhinney says, the EXACT answer to your SPECIFIC question was in the link he provided! In case you missed it, I looked it up for you:-

VB.NET:
Private Sub Clear()
    'Clear all unsaved lines.
    Me.lines.Clear()
    'Force the control to repaint so the lines are removed.
    Me.PictureBox1.Refresh()
End Sub

That's pretty self-explanatory to me. Had you been a complete noob to programming, jmcilhinney *might* have been a little more understanding, but as you pointed out that you have been a VB6 fan for years, I think he is perfectly entitled to expect you to read, digest and learn, rather than just pick out what you thought was relevant :)

Firstly - Rudeness is never acceptable - Period. On Line OffLine - Anywhere.
This attitude of "Ohh I was so busy building my hadron collider and you interupted me with your silly question" is downright juvenile and should not be tolerated anywhere.

I did not barge into the fellows home and ask my question.

I posted it on a web forum. If he has time to look at the thread then he has time to respond to it in a civilised manner or not at all.

Personally, I am far too busy to cruise forums like this looking for questions I already know the answer for. If people do so because they are motivated by a desire to impart knowledge then well and good.

Doing so because you want to get off on pretending you are the Don of MIT then you probably need to get out of the house a bit more and mix with some real people.

With regard to my question:

Actually I did not miss it. If you look at his code you will see that he is basically initialling his "lines" collection to have zero members and essentially letting the paint event draw nothing.

I sort of suggested that this would be a way of doing it with the prototype code I provided - "I could set the values for XP and YP very small but I'm sure there is a proper way of doing it"

INERTIAM - Perhaps you could show me how to implement "Me.Lines.Clear" in my code.
given I don't use a "Lines" class with a "clear" method!. Maybe you think the entire world should, forever more, use the sample code provided by jmcilhinney in any graphics work they do?. Have you even read the code I provided?

My question was - Is there a better way? If you read my code you will see that to draw a silly little circle on a form I do not create a class or a collection to do it.

My application is quite large and this is a little part of it. I've fixed it thanks.
Have a nice day. I am not waiting in desperation for your considered thoughts on the matter.

I know sqat about VB.NET GDI+ and I but I do know a little about dealing with people and talking down to them is cheap, immature and (when they are not physically in your face) it's actually kind of of pointless.

Just on the off chance that you don't get the message I really don't think I need any more help with this problem - Thanks Guys -Wonderful -Resolved -Keep up the good work!
 
You said in your first post that you want to draw multiple spots. As such you need to change your code so that it DOES use a collection. Then you can use the principle already described to clear that collection and this clears the drawing.
 
Back
Top