关于DialogResult
在程序中,经常会弹出一个对话框来让用户填写一些信息,填写完成后,当用户点击“确定”按钮后,在主窗体中进行其他的处理。比如一个简单的例子,在主窗体中有一个菜单,是“增加用户”,当点击这个菜单之后,我们需要弹出一个增加用户的窗体出来,就假设“增加用户”的窗体叫frmAddUser,那么代码如下:
1: frmAddUser frm = new frmAddUser();
2:
3: if (frm.ShowDialog() == DialogResult.OK)
4:
5: {
6:
7: //todo list
8:
9: }
10:
一般都是设置“增加用户”窗体中“确定”按钮的DialogResult的属性,
设置完成后,只要用户一点击“确定”按钮,那么对话框就关闭,重新回到主窗体,然后可以在主窗体中进行相应的处理,比如把数据写入数据库等。
现在有一个问题就是,“增加用户”的窗体如下:
如果我想判断一下,用户输入的邮件格式是否正确,那么我该把这段代码放在哪呢?如果放在邮件输入框那个TextBox的TextChaged事件中,那么用户输入一个字母都会激发那个事件,这样就不太好,如果放在“确定”按钮的事件中,那么如果用户的邮件格式输入的不正确,就不应该返回主窗体,而是继续留在这个窗体上,等待用户修改,可是事实却不是这样,因为你设置了“确定”按钮的Dialogue属性,只要用户一点击按钮,主窗体中的
1: if (frm.ShowDialog() == DialogResult.OK)
这个判断就为真,不管你的邮件格式是否正确,主窗体都会继续向下执行。
最后,终于找到了一个比较好的解决办法,都以为只有Button才有Diagolue属性,没想到窗体也有这个属性。所以我们根本就不需要设置“确定”按钮的Diagolue属性,直接把判断邮件是否合格的方法放在”确定”按钮的点击事件中,如果符合,那么就设置窗体的Diagolue属性为OK,用下面的代码:
1:
2: this.DialogResult = DialogResult.OK;
这样主窗体中的if (frm.ShowDialog() == DialogResult.OK)这个判断也为真,它会继续执行下面的代码。如果不符合格式,就啥也不做,因为没有设置窗体的Diagolue属性为OK,所以“增加用户”的对话框永远不会关闭,主窗体也会一直等待,而不会去执行其他的代码。
以上为我转的,解释的很好
--------------------------------------------------------------------------------------
我实验的:
在form1中写button事件:
Form2 f2 = new Form2();
f2.Show();
f2.Visible = false;//没有这句的话,下面的ShowDialog()会出异常 。已经可见的窗体不能显示为模式对话框。在调用 showDialog 之前应将窗体的 Visible 属性设置为 false。
if (f2.ShowDialog() == DialogResult.OK)
{
this.button1.Text = f2.Text;
}
或者:
Form2 f2 = new Form2();
if (f2.ShowDialog() == DialogResult.OK)
{
this.button1.Text = f2.Text;
}
form2中 button事件:
DialogResult = DialogResult.OK;
模态对话框:Modal Dialogue Box,是指在用户想要对对话框以外的应用程序进行操作时,必须首先对该对话框进行响应。如单击【确定】或【取消】按钮等将该对话框关闭。
--------------------------------------------------------------------------------------
windows 的showDialog 方法
窗体的对话框结果是当窗体显示为模式对话框时从该窗体返回的值,如果窗体显示为对话框,用DialogResult枚举中的值设置此属性将设置该窗体的对话框结果值、隐藏模式对话框并将控制返回给调用窗体。此属性通常由窗体上Button控件的DialogResult属性设置
当用户单击 Button 控件时,分配给 Button 的DialogResult 属性的值将分配给该窗体的 DialogResult 属性。
当窗体显示为模式对话框时,单击“关闭”按钮(窗体右上角带 X 的按钮)会隐藏窗体并将 DialogResult 属性设置为 DialogResult.Cancel。当用户单击对话框的“关闭”按钮或设置 DialogResult 属性的值时,不会自动调用 Close 方法。而是隐藏该窗体并可重新显示该窗体,而不用创建该对话框的新实例。因为此行为,所以当应用程序不再需要该窗体时,必须调用该窗体的 Dispose 方法。
可以使用此属性确定对话框是如何关闭的,以便正确处理在该对话框中执行的操作。
{
// Create a new instance of the form.
Form form1 = new Form();
// Create two buttons to use as the accept and cancel buttons.
Button button1 = new Button ();
Button button2 = new Button ();
// Set the text of button1 to "OK".
button1.Text = "OK";
// Set the position of the button on the form.
button1.Location = new Point (10, 10);
// Set the text of button2 to "Cancel".
button2.Text = "Cancel";
// Set the position of the button based on the location of button1.
button2.Location
= new Point (button1.Left, button1.Height + button1.Top + 10);
// Make button1's dialog result OK.
button1.DialogResult = DialogResult.OK;
// Make button2's dialog result Cancel.
button2.DialogResult = DialogResult.Cancel;
// Set the caption bar text of the form.
form1.Text = "My Dialog Box";
// Define the border style of the form to a dialog box.
form1.FormBorderStyle = FormBorderStyle.FixedDialog;
// Set the accept button of the form to button1.
form1.AcceptButton = button1;
// Set the cancel button of the form to button2.
form1.CancelButton = button2;
// Set the start position of the form to the center of the screen.
form1.StartPosition = FormStartPosition.CenterScreen;
// Add button1 to the form.
form1.Controls.Add(button1);
// Add button2 to the form.
form1.Controls.Add(button2);
// Display the form as a modal dialog box.
form1.ShowDialog();
// Determine if the OK button was clicked on the dialog box.
if (form1.DialogResult == DialogResult.OK)
{
// Display a message box indicating that the OK button was clicked.
MessageBox.Show("The OK button on the form was clicked.");
// Optional: Call the Dispose method when you are finished with the dialog box.
form1.Dispose();
}
else
{
// Display a message box indicating that the Cancel button was clicked.
MessageBox.Show("The Cancel button on the form was clicked.");
// Optional: Call the Dispose method when you are finished with the dialog box.
form1.Dispose();
}
}
关于DialogResult相关推荐
- c# dialogresult 选择文件_C#实战037:一个简单的入门应用程序NXPlug install
前面通过一系列的NX二次开发已经基本完成了项目对NX的需求了,接着就是将插件打包给用户使用了.使用过程很简单只要将文件拷贝至任意目录并添加环境变量UGII_USER_DIR即可,为了方便一般都会放在N ...
- WPF使用SaveFileDialog对话框保存文件时不要用DialogResult.OK判断对话框正常关闭
WPF使用SaveFileDialog对话框保存文件 ,使用下面代码判断对话框正常关闭会报DialogResult没有OK值的错误. if (saveFileDialog.ShowDialog() = ...
- C# DialogResult的用法
在程序中,经常会弹出一个对话框来让用户填写一些信息,填写完成后,当用户点击"确定"按钮后,在主窗体中进行其他的处理.比如一个简单的例子,在主窗体中有一个菜单,是"增加用户 ...
- DialogResult的用法
在程序中一旦设置DialogResult的值,接下来就会自动执行close()的方法,所以没有必要写this.close(),并且也没有必要设定closing和closed事件,其执行顺序为Dialo ...
- DialogResult
C#窗口调用时,对DialogResult的判断需要注意:(不知道是不是编译器问题,确实很古怪) 窗口中的button.DialogResult与form.DialogResult不同. 只有当sho ...
- 关于DialogResult的用法
在程序中,经常会弹出一个对话框来让用户填写一些信息,填写完成后,当用户点击"确定"按钮后,在主窗体中进行其他的处理.比如一个简单的例子,在主窗体中有一个菜单,是"增加用户 ...
- #利用DialogResult属性实现主程序的打开当前窗口的关闭
利用DialogResult属性实现主程序的打开当前窗口的关闭 首先介绍一下非模式化窗体show()和模式化窗体showdialog()的概念: 两种方法都能打开显示窗体, 1.非模式化窗体show( ...
- C# 第六章『交互式图形界面』◆第5节:FolderBrowserDialog类、DialogResult枚举
一.FolderBrowserDialog 类 FolderBrowserDialog 类https://docs.microsoft.com/zh-cn/dotnet/api/system.wind ...
- C#第七回:DialogResult
一直没太搞明白这个逻辑是什么意思,今天无意中看到个例子,终于小小的理解了一下. 下面这段代码很好理解, //create a new dialog, and get the value by usin ...
- Form 设置 DialogResult后 窗体会自动关闭
问题:有一个逻辑是点击保存按钮时,仅设置 DialogResult = OK,而不用关闭窗体,但是点击事件结束后窗体仍然自动关闭. 原因:使用 ShowDialog 方式弹出窗体时,只要设置了 Dia ...
最新文章
- javascript高级程序设计(第3版)之《script元素》
- pthread_mutex_lock的thread特性
- 局域网抓包分析工具_[源码和文档分享]基于Libpcap实现的局域网嗅探抓包发包解析工具...
- mysql5.5连接器_MySQL :: MySQL 5.1参考手册 :: 26. 连接器
- 计算机基础原理知识,计算机基础知识之计算机的工作原理
- mysql创建文章表_创建表--文章表article
- SQLServer全文检索无内容
- 折线图 java_java报表折线图
- java string format s_JAVA字符串格式化-String.format()的使用
- stm32F051系列教程 前哨篇 建立一个KEIL工程模板
- flask使用sqlit3的两种方式
- 深井软岩巷道群支护技术与应用_金能煤业公司组织观看千米深井软岩巷道大变形机理及围岩控制技术讲座...
- matlab——矩阵运算
- vector subscript out of range数组下标越界错误
- 租用国外服务器应该注意哪些?
- rrpp协议如何修改_RRPP(快速环网保护协议)
- 配置失败还原请勿关闭计算机怎么办,配置windows update失败还原更改 彻底解决方案...
- 单片机I/O常用的驱动与隔离电路设计
- Linux KVM环境搭建,以及创建kvm虚拟机
- Transform.RotateAround 围绕旋转