I'd like to show an input modal in my WinForm application. I have looked around the web, but haven't found a good pattern for doing this. I understand I'd have to create another Form, and use the ShowDialog method.
            Asked
            
        
        
            Active
            
        
            Viewed 1.0k times
        
    4
            
            
        - 
                    http://www.reflectionit.nl/Articles/InputBox.aspx has an example. – Preet Sangha Aug 28 '12 at 10:22
- 
                    https://stackoverflow.com/a/17546909/740639 is another example Input Prompt class that you can copy and paste. – Walter Stabosz Apr 15 '20 at 15:43
1 Answers
15
            You are correct.
Note that modal dialogs are not automatically disposed when closed (unlike non-modal dialogs), so you want a pattern like:
using (FrmModal myForm = new FrmModal())
{
    DialogResult dr = myForm.ShowDialog();
    if (dr == DialogResult.OK)
    {
        // ...
    }
    else
    {
        // ...
    }
}
In the new form itself (which I have called FrmModal), set the DialogResult property in your button event handlers appropriately, e.g. if you have an OK button you would want to set DialogResult = DialogResult.OK in the event handler for that button and then call Close() to close the form.
 
    
    
        Eric J.
        
- 147,927
- 63
- 340
- 553
- 
                    1Small comment: Setting a Forms DialogResult != none is enough to close it. – H H Sep 07 '09 at 08:37
