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.