windows 的showDialog 方法

http://msdn2.microsoft.com/zh-cn/library/system.windows.forms.form.dialogresult(VS.80).aspx

 

窗体的对话框结果是当窗体显示为模式对话框时从该窗体返回的值。如果窗体显示为对话框,用 DialogResult 枚举中的值设置此属性将设置该窗体的对话框结果值、隐藏模式对话框并将控制返回给调用窗体。此属性通常由窗体上 Button 控件的 DialogResult 属性设置。当用户单击 Button 控件时,分配给 ButtonDialogResult 属性的值将分配给该窗体的 DialogResult 属性。

当窗体显示为模式对话框时,单击“关闭”按钮(窗体右上角带 X 的按钮)会隐藏窗体并将 DialogResult 属性设置为 DialogResult.Cancel。当用户单击对话框的“关闭”按钮或设置 DialogResult 属性的值时,不会自动调用 Close 方法。而是隐藏该窗体并可重新显示该窗体,而不用创建该对话框的新实例。因为此行为,所以当应用程序不再需要该窗体时,必须调用该窗体的 Dispose 方法。

可以使用此属性确定对话框是如何关闭的,以便正确处理在该对话框中执行的操作。


public void CreateMyForm()
 
{
    
// 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 (1010);
    
// 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();
    }

 }

    

posted on   simhare  阅读(4472)  评论(0编辑  收藏  举报

编辑推荐:
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
阅读排行:
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· Vite CVE-2025-30208 安全漏洞
· 《HelloGitHub》第 108 期
· MQ 如何保证数据一致性?
· 一个基于 .NET 开源免费的异地组网和内网穿透工具

导航

< 2007年10月 >
30 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31 1 2 3
4 5 6 7 8 9 10
点击右上角即可分享
微信分享提示