Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# showdialog

作者:潮起潮落   发布日期:2026-02-23   浏览:79

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;
        }
    }
}

解释说明:

  1. 创建按钮:我们首先创建了一个按钮,并设置了它的文本和点击事件处理程序。
  2. 创建窗体:然后创建了一个窗体,并将按钮添加到窗体的控件集合中。
  3. 显示对话框:使用 ShowDialog() 方法以对话框模式显示窗体。这会使窗体模态化,即在关闭该窗体之前,用户无法与应用程序的其他部分交互。
  4. 检查对话框结果:根据用户点击的按钮(如确定或取消),我们可以获取 DialogResult 来判断用户的操作。
  5. 按钮点击事件:当用户点击按钮时,设置窗体的 DialogResultDialogResult.OK,从而关闭对话框并返回结果。

希望这段代码和解释对你有帮助!

上一篇:c# 字符串转int

下一篇:c# sm4加密

大家都在看

c# 二进制

c# 创建目录

c# datatable group by

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#获取系统时间

c#游戏开发

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站