using System;
using System.Windows.Forms;
namespace ShowDialogExample
{
public class Program
{
[STAThread]
public static void Main()
{
// 创建一个按钮
Button button = new Button();
button.Text = "Click Me";
button.Click += Button_Click;
// 创建一个窗体并添加按钮
Form form = new Form();
form.Controls.Add(button);
// 显示窗体为对话框模式
DialogResult result = form.ShowDialog();
// 检查用户点击了哪个按钮(确定或取消)
if (result == DialogResult.OK)
{
Console.WriteLine("User clicked OK");
}
else if (result == DialogResult.Cancel)
{
Console.WriteLine("User clicked Cancel");
}
}
private static void Button_Click(object sender, EventArgs e)
{
// 设置对话框结果为OK,表示用户点击了按钮
((Form)((Button)sender).Parent).DialogResult = DialogResult.OK;
}
}
}
ShowDialog() 方法以对话框模式显示窗体。这会使窗体模态化,即在关闭该窗体之前,用户无法与应用程序的其他部分交互。DialogResult 来判断用户的操作。DialogResult 为 DialogResult.OK,从而关闭对话框并返回结果。希望这段代码和解释对你有帮助!
上一篇:c# 字符串转int
下一篇:c# sm4加密
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站