Mantain state in forms and avoid kill handlers

  • Thread starter Thread starter Nicolás Castagnet
  • Start date Start date
N

Nicolás Castagnet

Hi,



I have a form with a lot of ComboBox (with a lot of items), and it is used
very frequently. So, I want to reduce the latency to show the form.



I test to assign the instance of the form after close in a static field, and
show it again (with ShowDialog). This approach reduce the time to half
(although, the handles are destroyed when the form is closed, and recreated
when it is showed again). But I notice some problems (for example, the
MouseEnter and MouseLeave events are not fired over controls the second time
I show the form).



Other requirements:

- The form must be modal (so, I can't use a Show/Hide methods)

- I need to persist the state in the form (I need to keep the selected
options)



Any advice?



Thanks,

Nicolás
 
Hi Nicolas,

You can intercept the Closing event, cancel it and then do the assignment
to the static field (to ensure the form instance is not disposed of):

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
this.Hide();
<StaticField> = this;
}

You might also consider to implement the form as an "everlasting" singleton
encapsulating the logic completely inside the form class.

Best regards,

Palo
 
Reproduced!

You're right, the MouseLeave event won't fire after the form is hidden and
show again.
I've checked other controls (Label, TextBox) and they behave the same way.

Seems to be bug, because the events MouseEnter/Leave events are still hooked
after
recreating the form, but MouseLeave doesn't fire???

Could someone from MS check it out?

Regards,

Palo

--
http://dact.lamarvin.com/
The first AutoComplete component for .NET framework Windows Forms
applications
http://www.vbinfozine.com/
An ordinary VB developer shares his own successes and failures
 
Back
Top