如何在C#中优雅地处理确认对话框

   2024-09-30 3520
核心提示:在C#中,可以使用Windows Forms或WPF来创建一个确认对话框使用Windows Forms的MessageBox类:using System;using System.Windows

在C#中,可以使用Windows Forms或WPF来创建一个确认对话框

使用Windows Forms的MessageBox类:
using System;using System.Windows.Forms;namespace ConfirmDialogExample{    class Program    {        [STAThread]        static void Main(string[] args)        {            DialogResult result = MessageBox.Show("你确定要继续吗?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);            if (result == DialogResult.Yes)            {                Console.WriteLine("用户点击了'是'按钮");            }            else if (result == DialogResult.No)            {                Console.WriteLine("用户点击了'否'按钮");            }        }    }}
使用WPF的MessageBox类(需要引用System.Windows.Forms.dll):
using System;using System.Windows;using System.Windows.Forms;namespace ConfirmDialogExample{    class Program    {        [STAThread]        static void Main(string[] args)        {            MessageBoxResult result = MessageBox.Show("你确定要继续吗?", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question);            if (result == MessageBoxResult.Yes)            {                Console.WriteLine("用户点击了'是'按钮");            }            else if (result == MessageBoxResult.No)            {                Console.WriteLine("用户点击了'否'按钮");            }        }    }}

这两种方法都会显示一个包含“是”和“否”按钮的对话框。根据用户的选择,程序将执行相应的操作。请注意,为了使用这些功能,您需要添加对System.Windows.FormsSystem.Windows的引用。

 
举报打赏
 
更多>同类物流大全
推荐图文
推荐物流大全
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号