Show input dialog in WinForms

守給你的承諾、 提交于 2019-12-22 08:20:40

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/1387236/show-input-dialog-in-winforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!