【问题标题】:Is there a builtin confirmation dialog in Windows Forms?Windows 窗体中是否有内置确认对话框?
【发布时间】:2011-04-20 05:30:52
【问题描述】:

我想创建一个简单的确认对话框,上面写着“请检查信息,如果您确定信息正确,请单击确定。”

有这样的内置东西吗?

【问题讨论】:

    标签: c# winforms dialog


    【解决方案1】:

    .Net Core 你可以这样做:

    DialogResult dialogResult= MessageBox.Show("Are you sure to delete?", "Confirm", MessageBoxButtons.YesNo);
    
    if (dialogResult == DialogResult.Yes)
    {
        //if code here....            
    }
    else
    {
       //else code here.... 
    }
    

    Output Result

    【讨论】:

    • 实际上该示例是 .Net 2.x、3.x 和 4.x 而不是 .Net Core。在 .Net Core 中,您无法创建 WinForms。从 .Net Core 3.0 开始,您虽然有 XAML 选项,但我不确定您是否以相同的方式创建消息框。
    【解决方案2】:

    这是一个例子。你可以试试这样的。

    var confirmResult =  MessageBox.Show("Are you sure to delete this item ??",
                                         "Confirm Delete!!",
                                         MessageBoxButtons.YesNo);
    if (confirmResult == DialogResult.Yes)
    {
        // If 'Yes', do something here.
    }
    else
    {
        // If 'No', do something here.
    }
    

    您也可以尝试用MessageBoxButtons.OKCancel 代替MessageBoxButtons.YesNo。这取决于您的要求。

    1. 如果您有 .Net Framework 4.6 或更高版本,请试试这个。
    MessageBoxResult confirmResult = MessageBox.Show("Are you sure to delete this item ??", "Confirm Delete!!", MessageBoxButton.YesNo);`
    
    if (confirmResult == MessageBoxResult.Yes)
    {
       // If 'Yes', do something here.
    }
    else
    {
       // If 'No', do something here.
    }
    

    【讨论】:

    • 这种方法对我来说非常有效。我有一个案例,其中有一个“重置”按钮可以删除数据,这提供了一种很好的处理方法。
    • 在我的情况下,我不得不使用 MessageBoxResult 而不是 DialogResult
    • MessageBoxButtons.YesNo 应为 MessageBoxButton.YesNo,并且 .NET 4.6 的 DialogResult 更改为 MessageBoxResult。
    【解决方案3】:

    MessageBox.Show?您可以指定要显示的按钮的标题、标题和一些选项。

    另一方面,如果您要求人们确认信息,听起来您可能想要显示一个自定义对话框 - 您可以使用 Form.ShowDialog 来完成。

    【讨论】:

    • 谢谢,我会研究 Form.ShowDialog 类。顺便说一句,你是如何在不显示修订的情况下编辑你的问题的?
    猜你喜欢
    • 2023-03-17
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-10
    相关资源
    最近更新 更多