area wiped by new form

SSb

Member
Joined
Sep 28, 2005
Messages
14
Programming Experience
Beginner
Hi all,
Here is what I'm doing briefly,

There is a Form-A maximized on the screen, the user clicks a button and a new form comes up

'Within the buttons Click Sub I have these lines
Dim sfrm As New SettingsForm 'The new form
sfrm.ShowDialog() ' User gives values and closes it by clicking Ok on it
sfrm.Dispose() ' Doesn't seem to have an effect on what I'm trying to do
magphplot() ' Some plots are drawn on the original form - A
End Sub

The plots are being drawn on a PictureBox.

What I find is that any portion of the area where the plots are being drawn, which overlaps with the area where the SettingsForm was opened are wiped clean immediately. Eventhough plotting is supposed to be done after this SettingsForm is closed.

Also when the SettingsForm is dragged over the plot area the plots there are wiped.

How can I avoide these things ?

Is there a way of getting a snapshot of the current form before opening a new form and then restoring that snapshot-picture back when the new form is closed. Without having to write the complete drawing routine in the Paint event.

Thanks,
Saurabh
 
I suspect you do some kind of CreateGraphics, which is not permanent drawing. If you wish to preserve anything drawn you must either keep track of what and continually draw the same things in Paint handler or draw to an image/bitmap and display it. The Picturebox is good at displaying images.
 
plotting through the Paint Sub

Hi,

I tried to use the paint routine. I call the function which I use to plot on the picturebox from within the Paint Sub of the form. However each time I minimize the form and maximize it again the plots seem to be drawn for just a split second and then they are gone. The same happens when the another form which open on top of this main form is closed.

However if I put a line
MessageBox.Show("some text")
at the very begining of the Paint Sub things work better. Now when I minimize and then maximize the form, the MessageBox comes up and after I press Ok the plots are drawn and they stay that way. Just the place where the MessageBox appeared is wiped.

Any suggestions as to why this might be happening?

Thanks,
Saurabh
 
Hi,
application.Doevent did not help. I even tried Me.Invalidate and Me.Refresh just to trigger the pain event after closing and disposing the old form. However each time the old form closes it still wipes out the area of the plot which lies behind it.

Saurabh
 
Maybe you should post some code that shows how? The paint event handler and connected method in its simplicity should be enough. Just one thought, I suddenly noticed you said 'paint method of form to draw on picturebox' That's wrong, use the Paint event of Picturebox to draw on picturebox. This also probably means you used CreateGraphics which you shouldn't.
 
code

Hi,
Here is a simplifed version of my program.
Form 1 has a PictureBox1 with a white background and a red filled ellipse. This background and ellipse are drawn from within the paint Sub of Form 1. By clicking a button on Form 1 anothe form Form 2 comes up and it can be closed.

What happens is the following. After Form 2 is closed the ellipse is drawn but the portion where Form 2 was present is left white. Also if the MessageBox line is used in the Paint Sub of Form 1 then only the portion behind the MessageBox is left white.

Also the background and ellipse are drawn and cleaned when the form first loads.

The things remain the same irrespective of the Try ... End Try block in the Paint Sub.

Thanks,
Saurabh

Code:
Within Form1 - Button used to open Form 2

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f2 As New Form2
f2.ShowDialog() 'Tried with and without
f2.Dispose() 'Tried with and without
Me.Invalidate() 'Tried with and without
End Sub

Within Form1 - Paint Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
'MessageBox.Show("MessageBox from the first line, inside Paint Sub of Form 1")
Dim paper As Graphics
paper = PictureBox1.CreateGraphics()
Dim erasebrush As Brush = New SolidBrush(System.Drawing.Color.White)
Dim erec As Rectangle
erec = New Rectangle(1, 1, PictureBox1.Width, PictureBox1.Height)
Dim gbrush As Brush = System.Drawing.Brushes.CadetBlue
Try
paper.FillRectangle(erasebrush, erec)
paper.FillEllipse(gbrush, 1, 1, PictureBox1.Width, PictureBox1.Height)
Finally
paper.Dispose()
End Try
End Sub

Within Form2 - for the closing button

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
 

Attachments

  • Frm1.zip
    9.8 KB · Views: 20
Yeah, wrong Paint event dude, and don't use CreateGraphics. In PictureBox1 Paint event handler you draw with e.Graphics.
 
Back
Top