Window Flashing !!!

  • Thread starter Thread starter Paul Loveless
  • Start date Start date
P

Paul Loveless

Hi all. I've noticed in my dot net program when the main program window is
maximized, and I close an open form (well technically I hide it) that I have
displayed in the main window, that the main window will occasionally flash
momentarily, displaying another open program. It is like windows xp is doing
a context switch with another open program, momentarily making it the active
window and then quickly switching back to my program. On a fast computer,
the flash is very quick and hardly noticeable, but on a slower computer, it
is quite noticeable. However, when I close a message box displayed in the
main window, the flash never occurs.

I've also noticed that other non-dot net programs don't seem to do this. So,
what causes this? Is it the dot net runtime environment, is my program's
time slice up causing the os to perform a context switch (which I doubt) or
is it something else?

I'm curious to know if anyone knows why this happens and if there is a way
to stop it from occurring. Thanks in advance to any replies.

Paul
 
Couple of questions:
1. Are you displaying the form modal or modelessly?
2. Are you passing in the main form as the owner window when you show the
fom?
 
Form is modal; displayed as: form.ShowDialog(this);

As I mentioned in my earlier msg, I'm hiding the form when you click a
button, not closing it. While dicking around some more, I found that if I
use the Form.Close() method instead, it eliminates the flashing. However,
that raises another point. Doesn't calling Close() destroy the form object
(or at least prepare it for garbage collection)?

Paul
 
Ah yeah, I remember that now.
Closing the form doesn't destroy the object at all, it just destroys the
windows handle associated with the form. The object is still accessible, as
are any controls on the form.
 
John Wood said:
Ah yeah, I remember that now.
Closing the form doesn't destroy the object at all, it just destroys the
windows handle associated with the form. The object is still accessible, as
are any controls on the form.

That's interesting. The problem of the Close vs Hide methods has never been
very clear to me. The msdn doc for the Close() method says:
"When a form is closed, all resources created within the object are closed
and the form is disposed" and "When the Close method is called on a Form
displayed as a modeless window, you cannot call the Show method to make the
form visible, because the form's resources have already been released". And
it doesn't say anything about modal windows...

Isn't that the opposite of what you've just said? Or am i missing something?
What exactly happens when you call Close on a modal window? Is the window
disposed or not? What happens if i call ShowDialog on a Form that has
previously been closed with the Close method. Do i need to call the Dispose
method at some point or not? If i want to access to some properties of a
form *after* it has been closed, do i need to call the Hide method to hide
it or can i safely call the Close method?

Thanks
 
Which was why I used the .Hide method. I thought .Close would destroy the
form and to re-display it, would require creating a new object every time.
This would certainly occur if the form was modeless and I called .Close, but
the form is only hidden if it is modal. Substituting .Close for .Hide() also
eliminates the annoying flash that sometimes occurs when you close (i.e.
hide) the form (maybe you have seen this before?).

Anyway, annoyance solved, time to move on. I thank you for your replies
John.

Paul
 
Yea, I was initially confused over the text regarding the .Close method in
the msdn documentation too. Navigate over to the Form.ShowDialog method and
this is what it will say about closing a modal form:

"When a form is displayed as a modal dialog box, clicking the close form
button (the button with an "X" at the top right of the form) causes the form
to be hidden...Unlike modeless forms, the Close method is not called by the
..NET Framework when the user clicks the close form button of a dialog box or
sets the value of the DialogResult property. Instead the form is hidden and
can be shown again without creating a new instance of the dialog box.
Because a form displayed as a dialog box is not closed, you must call the
Dispose method of the form when the form is no longer needed by your
application."

So, the functionality of the close button (X) depends on whether the form
being shown is modal or modeless. If it is modal, the form will be hidden
and not disposed. Thus, you can have a button on the form that calls the
Close method when clicked, and the form will really be hidden. I know, it's
strange and has caused me confusion. Your modal form will actually be
disposed when your form variable goes out of scope or you explicitly dispose
it.

However, if the form is modeless, clicking the close button or calling the
Close method will indeed close and dispose of the form. If you attempt to
show the form again, you will get a run-time error. Thus, when I want to
show/close a form, I use the following rules:

To show a modal form:
call Form.ShowDialog()

To hide a modal form:
call Form.Close() on any button controls; the (X) button will do it
automatically (and does not actually close/dispose the form).

To show a modeless form:
call Form.Show()

To hide a modeless form (2 methods):
1) turn off the ControlBox, so the (X) button is not shown (remember, the
(X) button will close and dispose the form).
call Form.Hide from a button control

2) leave the ControlBox on
call Form.Close from a button; this will cause the Form.Closing event to
fire. In this event, I call Form.Hide and set the cancel property to true.

Hope this helps to clear up some of the confusion.

Paul
 
I've seen this problem myself and have not been able to identify why it
happens. However, just like you, I noticed that it happened when I hid
other forms in my application. I'm developing mostly on a fast computer so
I don't know if my fix corrected the problem or just made it less visible,
but I'll pass it on anyway. I found that if I activated the parent form
when I closed the child form then I didn't see the problem anymore. I don't
know if it's relevant to the problem, but in my case I was using "owned"
forms. My code was like this:

ChildForm.Hide();
ParentForm.Activate();

Hope this helps.

- Rhy
 
Back
Top