简体   繁体   中英

Form.ShowDialog() Without Destroying Handle

I am trying to show a form using Form.ShowDialog as shown below:

var f = new Form();
if(f.ShowDialog() == DialogResult.OK)
{
     ...
}
...
if(f.ShowDialog() == DialogResult.OK)
{
     ...
}

The issue is the OnHandleDestroyed is called once a dialog result is returned and the form is closed.

  • Why do I care about OnHandleDestroyed? I have an OpenGL control on the form, and it disposes the Context when OnHandleDestroyed is called.
  • Why don't I dispose of the form, and use ShowDialog on the new form? I am trying to reuse the form as loading the form is slow - but populating it with data is quick.

So the question is: Is it possible to use ShowDialog() without closing the form (and hiding it instead) OR to show a form modally using Show() and Hide()?

When you show a form using ShowDialog , after closing the form DestroyHandle will be called automatically.

To prevent the behavior you can override DestroyHandle method and write your own logic.

Example

public class MyForm : Form
{
    protected override void DestroyHandle()
    {
        if (!Modal || Disposing)
            base.DestroyHandle();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM