Dim ShouldPaintImage2
as
boolean=
true
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
e.Graphics.DrawImage(My.Resources.image1,
20
,
20
)
if
ShouldPaintImage2=
true
then
[remove old graphic]
e.Graphics.DrawImage(My.Resources.image2,
20
,
20
)
End Sub
Is it possible to remove
only the graphic of image1
so i can change the image to image2?
Thanks
Yes of course. You need to understand that
OnPaint
, which you are correctly using, is called on the Windows
WM_PAINT
messages, which your UI receives on each invalidation:
http://msdn.microsoft.com/en-us/library/598t492a.aspx
[
^
].
Please see my past answers:
Drawing Lines between mdi child forms
[
^
],
capture the drawing on a panel
[
^
],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))
[
^
],
How to speed up my vb.net application?
[
^
].
That said, all you need is this: Draw not a fixed image, but a variable image/bitmap reference. Make a reference to
Image
an instance field of, say, your form class; let's say, its name is
MyImage
. Then your line with
DrawImage
should be changes to
e.Graphics.DrawImage(MyImage ...)
.
Now, the procedure of "switching the image" will look like
MyImage =
self.Invalidate();
Are you getting the idea?
I think you're going about this the wrong way, wouldn't it make more sense to just not draw it in the first place if that's the case? i.e.:
Dim
ShouldPaintImage2
as
boolean=true
Protected
Overrides
Sub
OnPaint(
ByVal
e
As
PaintEventArgs)
If
ShouldPaintImage2=true
Then
e.Graphics.DrawImage(My.Resources.image2,
20
,
20
)
e.Graphics.DrawImage(My.Resources.image1,
20
,
20
)
End
If
End
Sub
(Sorry if this isn't perfectly correct syntax, my VB.Net is very rusty)
Otherwise, to my knowledge you can't erase a specific object, if your real code is more complicated, clear the drawing, then redraw the whole thing, using image2 instead of image1.
Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad
spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or
edit the question
and fix the problem. Insults are not welcome.
Don't tell someone to read the manual. Chances are they have and don't get it.
Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
"In reverse" is irrelevant, because after each invalidation, all the rendering is redone from the very beginning, in whatever order. I explained it in my answer.
—SA