Erase text

Adagio

Well-known member
Joined
Dec 12, 2005
Messages
162
Programming Experience
Beginner
How do you erase text written with the drawstring on a graphics object?

The only way I could think would be to simply take the background image and overdraw the area of the text with the drawimage (I have the height, width and position of the text, and the background image), but unfortunatly the drawimage only draws in the background, so that text/lines/etc wont be erased, but the background will be changed (if I use a different picture than the current background)

How can you get it to draw it infront of the text I have written on the same graphics object?
 
Hi adagio, You don't need to draw it infront you just need to set the text to "" and whether your talking about doing this at design time or run-time it's the same. i Would create a property something like...

VB.NET:
Public Property GraphicsText as String
Get
 Return YourTextVariable
End Get
Set (ByVal value as String)
YourTextVariable = Value
me.graphicsobject.refresh
End Set
End Property

Then you can set the text to an empty string or anything you like for that matter and it will redraw itself because of the Me.Refresh inside the Set Block.
 
Thank you, it works if I use the temp* undraw method, which is not that good
The problem is that I can't find refresh in the Graphics object I use :confused:

* But this sub is setup so that it creates a new object with the background, and completely redraws everything :(
 
Last edited:
You draw this string on the Paint event. You should have a boolean variable that tells you whether to draw the string or not, which you would test in the Paint event handler. If the variable is True then draw the string, otherwise don't. Then when you want to erase the string you just set the variable to False and Refresh the form.
 
For example:
VB.NET:
Private textDrawn As Boolean = False

Private Sub DrawText()
    Me.textDrawn = True
    Me.Refresh()
End Sub

Private Sub EraseText()
    Me.textDrawn = False
    Me.Refresh()
End Sub

Private Sub Form1_Paint(...) Handles MyBase.Paint
    If Me.textDrawn Then
        e.Graphics.DrawString(...)
    End If
End Sub
 
I'm not sure how to call the Paint event, so I can't test it, but it doesn't really seem like it's possible to change my code to have the text to be written in the form

Here's what I have:

Class1:
Friend Shared backgroundImage As Image
Friend Shared winGameField As GameField
Friend Shared DrawGraphics As Graphics

Sub New(ByRef gf As GameField)

winGameField = gf
DrawBitmap = New Bitmap(gf.imgGameField.Width, gf.imgGameField.Height)
DrawBitmap = Bitmap.FromFile(Application.StartupPath + "\Images\DefaultBG.jpg")
DrawGraphics = Graphics.FromImage(DrawBitmap)
gf.imgGameField.Image = DrawBitmap
backgroundImage = gf.imgGameField.Image

Dim text As New Class2("Testing", New Font("Arial", 20))
text.draw()
text.Text = "Test Sting"
text.undraw()
text.draw()
End Sub


Class2: (Inherits Class1)

Private strText As String
Private font As Font
Private location As Point

Public Property Text() As String
Get
Return strText
End Get
Set(ByVal value As String)
strText = value
winGameField.Refresh()
End Set
End Property

Sub New(ByVal text As String, ByVal font As Font, ByVal location As Point)
Me.strText = text
Me.font = font
Me.location = location
End Sub

Public Sub draw()
DrawGraphics.DrawString(strText, font, Brushes.Black, location.X, location.Y)
End Sub

Public Sub undraw()
Dim DrawBitmap2 = New Bitmap(500, 500)
DrawBitmap2 = Bitmap.FromFile(Application.StartupPath + "\Images\DefaultBG.jpg")
Dim backgroundImage2 = DrawBitmap2
backgroundImage = backgroundImage2
DrawGraphics.DrawImage(backgroundImage, New Point(0, 0))
End Sub



I have to load the background picture again, since nothing happens if I only run the last line in the undraw sub
I have to call undraw and draw each time I change the text, nothing else works (the refresh does nothing) :confused:


The GameField class is the form with an image
 
You don't "call" an event. An event is raised when something specific happens, e.g. the Paint event is raised on a control when that control is drawn on-screen. You can force a control, including a form, to be redrawn by calling Invalidate or Refresh. Your problem is that you've drawn directly on a Bitmap object, so redrawing will not overwrite what you drew previously. Generally you specifically choose to draw on a Bitmap object to avoid the issue of losing what was previously drawn. I'm no GDI+ guru, but I'd say that your only option would be to keep a backup copy of your Bitmap at each step to allow you to undo back to a previous stage. If you wanted to remove a particular step without affecting subsequent steps then you would have to go back to drawing on a control surface on a Paint event rather than drawing directly on a Bitmap.
 
Back
Top