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
Do you mean that you want to trigger from showing one image to another, during runtime? I would assume that, otherwise your would simply draw just one image...
"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
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 = ' different reference, ether image1 or image2... self.Invalidate(); ' this will eventually cause re-painting ' here, "self" is your control, of the class where you override OnPaint; ' it could be your form or whatever control it is...
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.
If I asked you how to add an event handler to a button would the answer require instructions on how to click a button to make the event fire? Of course it's required, but it's not what's being asked. Nothing in his question suggests he's having difficulty getting to that point, he's asking what do do once he's already there. If he also has difficulty getting it to repaint, it's his responsibility to ask for further clarification.
Right, but unfortunately, as far as I'm aware, the form does not track what steps you took to get the current visual state, so you cannot simple undo a step, you just clear the custom drawn part and redraw it with your new image. But you did mention something that gave me an idea...have you considered a PictureBox with a transparent background?
  • 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.
  •