I have a panel on form and i'm drawing a graph on it with a pen.When i open something on top of that form or i minimize the form and maximize it, the panel goes white until i draw something on it again.So i think i could call an event when my form goes inactive and active again or when i close anything that i have opened on top of that form.But my problem is that i don't know what should be a trigger for my event.Or if someone knows some better way!!!


your description is a little bit confusing but let me guess: trigger the refresh() method for the form:

this.Refresh(); // for immediate redraw of the form and it's components

your description is a little bit confusing but let me guess: trigger the refresh() method for the form:

this.Refresh(); // for immediate redraw of the form and it's components

OK i will do that but you didn't understood what is bodering me.I think i know how to redraw the graph but i don't know how will i know when to do it.I don't know how to tell the code:"OK the graph is beeing disrupted redraw it".

I dont know what to use for triggering the event!!!

for minimize/maximize try AutoSizeChanged event:

private void Form1_AutoSizeChanged(object sender, EventArgs e)

If you open other programs (windows) over the form i know what happenes. you can't do anything for that except 2 things i think:
1) try Activated event for the Focus of the form (because your form is not activated while you use other windows)
2) this really works but i's a little bit complicated. use a thread, BackgroundWorker to Refresh() you pannel. I can't tell the conde right now cause it's a little bit long. The problem is that if the thread refreshes the panel to at very little time intervals, the panel could flicker.

Hope at least one of these solution works.

sorry in my post i didn't mean AutoSizeChanged event but SizeChanged event:

private void Form1_SizeChanged(object sender, EventArgs e)

I'm not sure SizeChanged would fire if the window has just lost focus. I'd try using this.Activated or put an invalidate statement in the Paint event to refresh the graph object.

nelis, think about it.
What would happen if you put an invalidate statement in a Paint event handler?
Yes! It would raise a Paint event, which would call Invalidate which would...

ddanbe, on the contrary, it wouldn't do an infinite loop like you are suspecting. From your post I'm guessing you're talking about putting a Form.Invalidate(); in the Paint Event, however I didn't suggest doing that. I had stated to invalidate the graphing object. Try it yourself. Create a blank form with a rich textbox. Then do invalidate on the rich textbox.

int x=0;
private void Form1_Paint(object sender, PaintEventArgs e)
      rtb1.Text += "x: "+(x++)+"\n";

You will probably notice that only a few events come through (typically 3) when the form is focused. In my experience, the Paint event is only fired when the form object is painted, not the controls or objects within the form object. For example, if you make the form big and the rich textbox big, drag a small window over the rich textbox. You will notice that the Paint event is not firing. Next while dragging, just slightly move over a section of the form and the Paint event will fire.

This was why I suggested my path. Whatever object the graph is in, invalidate it from the Paint event of the form.

commented: was very halpefull +1

Thanks for enlightning me on the subject. But.
I believe in most of the cases you are right.
Why is it that I get x:44 or x:32 from times to times, when I execute what you said?
I would like to produce the exact circumstances when I got those, (just shuffling windows)
Any ideas?

Well thank you all for your help.I will try to test all of your suggestions.I hope it will work

Well this are the results:

The sizechanged event wasn't good because it was only working when i manually changed the form size.In other cases it didn't fired.

The Form_paint event was a cure for my problem.It he fired when i minimized or maximized the form and also when i opened some other window on top of my form so this was the solution of my problem

Thanks for your help!!!!

Well one more thing.This is just from curiosity.As i have seen the formPaint_event is firing on almost anything.I have tried to find an explanation on the msdn and it says the event is firing every time the control is redrawn.But when is it redrawn.

I thought that this event is firing only the first time the control is created but it looks it isn't so!!!!

The form is redrawn when any portion of the form object is required to be drawn. This could be from another window moving over the form or a focus/minimize/maximize. It would depend on the application. You can add a simple counter, sort of like the example I gave, to see how many times it's being fired in a given situation. This value may be different in Remote Desktop environments, I have not tested that.

ddanbe, it could be that the window is being unfocused and refocused by the windows environment. Each one causes a Paint event to fire. Perhaps it quickly does it and you see those results. In my current test environment, I consistently see three events at startup, and then from there it varies depending what I do.