Deleting Graphics

TwoWing

Well-known member
Joined
Jan 6, 2006
Messages
71
Location
Near Portsmouth, England
Programming Experience
Beginner
Hi.
I found in a book by Clayton Walnum a basic thing to do that I hadn't come across before - it is writing on a Form. I shall give the Coding after this writing. OK! I can do this and can change color, font and so on......... BUT, how can it be deleted or erased. I suppose this is the same with all Graphics?
I have no idea at all. How can you delete something that has been created with Graphics. Please, someone, show me how. :confused:
Dim g As Graphics = CreateGraphics()
Dim f As Font = Form1.DefaultFont
Dim B As New SolidBrush(Color.Black)
g.DrawString("Here's some text output.", f, B, 20, 10)
g.DrawString("Here's some more text output.", f, B, 20, 30)
g.DrawString("Here's still some more text output.", f, B, 20, 50)
Cheers, TwoWing
 
When you draw on a control you normally do so on the Paint event. That's because the next time the control is repainted your drawing will disappear, so you need to draw it again. This means that what you normally do is set up parameters to control the drawing at the class level and then access those parameters from the Paint event handler. Here's some code that will appear to draw and erase a string on a form using two buttons:
VB.NET:
    Private drawText As Boolean = False

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.drawText = True
        Me.Refresh()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.drawText = False
        Me.Refresh()
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        If Me.drawText Then
            e.Graphics.DrawString("Hello World", _
                                  New Font(FontFamily.GenericSansSerif.Name, 12), _
                                  Brushes.Black, _
                                  10, _
                                  10)
        End If
    End Sub
Calling Refresh on the form causes the Paint event to be raised. In the Paint event handler the string is only drawn if the drawText variable is True. This gives the impression of the string being drawn and erased, when in fact it is being erased whenever the form is repainted, and only drawn again if you have specified it to be so. Try adding this code to the Paint event handler and watch the Output window to see how often the form is repainted:
VB.NET:
        Static callCount As Integer

        callCount += 1
        Console.WriteLine("Paint event handler called " & callCount.ToString() & " times.")
 
Great!!!!! :D Thanks a lot. Cheers jmcilhinney.
I did as instructed. It was a treat to see!
So I did the same coding to the project (just a learning exercise) I showed a bit of - and it worked there, too. (And that was not in the Paint event!)It's just getting to know these things.
Also, I learned something else - the Console.WriteLine procedure.
Do I need to know what it meant by, The program ' [1100] ...... has exited .....code0(0x0) ?
VB.NET Forums is really good. Thanks a lot chaps!! ;)
 
The debugger will use the output window to give you various information. Much of it can be ignored in most circumstances. As for the exit code, you can have your application return an exit code which would be used to tell the calling process the result of the operation. Normally there is no calling process other than the operating sustem so this would not be used.
 
Hi, Again. Thanks. Can I ask a question to help me further, please?
I started a new project to put two Graphic objects on a Form then see if I could to the erase job. I put at the top:
Imports System.Drawing
Imports System.Drawing.Drawing2D
I then put this in:-
Private DrawText As Boolean = False

I then put this in to Form1_Paint
Dim gr As Graphics = e.Graphics
'Draw a green-filled rectangle.
gr.FillRectangle(Brushes.Green, New Rectangle(20, 10, 200, 100))
'Draw a blue-filled ellipse.
gr.FillEllipse(Brushes.Blue, 20, 150, 200, 100)
gr.FillPie(Brushes.Red, 320, 150, 200, 100, -45, 90)
gr.FillPie(Brushes.Pink, 320, 150, 200, 100, 45, 270)
End If
I then put the two codes for the button click events:-
Me.DrawText = True [False for the other one}
Me.Refresh()

It did not work. So I put this in around the Form1.Paint code:-
If Me.DrawText Then
End If

It worked.
Nothing wrong with using the IF.....End IF, but is there by any chance another method please?
Also, can a Graphical object be altered in size? Say, for instance, one of those above. I think Inflate is used, but I can't get anything that I can understand in MSDN.
Many thanks again. :)
PS Trying to get used to this text editor.
 
Think about what you're asking. The conditions controlling whether the drawing will be done are set outside the the paint event handler. Once you get to the Paint event handler the only way to know whether to draw ot not is to test whether those conditions are true or not. The logical way to do that is with an If statement.

As for Inflate, have you tries the example code the help topics provide to see how it behaves?
 
Cheers jmcilhinney, you're certainly getting me more adjusted to vb.NET - that's what I want and need! I did get something [re:Inflate] from MSDN. (A lot of the stuff in it still gets me confused!!!?? It seems to take things for granted that the user should know.
Anyway, you have given me some 'work' to get on with. If I get stuck, may I consult again? And there is another enigma I'd like resolved. But I had better give you a rest.
Many, many thanks. Again.
 
Back
Top