c# - Winform 消息框,如何禁用 C# 中的 YESNO 选项

标签 c# winforms messagebox

我想在消息框中显示 YesNoCancel 按钮,但同时我想禁用 YesNo 按钮并仅启用 Cancel 按钮。

我想这样做的原因是我正在做一个演示应用程序,我想向用户展示特定的功能是可用的,但同时我不想给他们保存访问权限。

以下是我的代码,现在介绍如何禁用 YesNo 按钮。

DialogResult result = MessageBox.Show("Save changes to " + this.Text.Substring(0, this.Text.Length - 1) + "?",
                                      "Save confirmation", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

实际上我想显示 YesNo 按钮,但我想禁用它的点击访问。我想向用户显示 3 个按钮"is"、“否”和“取消”,但单击访问权限应仅授予“取消”按钮。那可能吗?

编辑: 谢谢大家的回答。

我找到了我的问题的灵魂

我的自定义消息框代码,我希望这可以帮助某人

customMsgBox.cs

enter code here { public partial class CustomMsgBox : Form
{
    static CustomMsgBox MsgBox;
    static string Button_id;

    public CustomMsgBox()
    {
        InitializeComponent();
    }

    internal static string ShowBox(string txtMessage, enumMessageIcon messageIcon)
    {
        MsgBox = new CustomMsgBox();
        MsgBox.labelCustomMsg.Text = txtMessage;
        MsgBox.addIconImage(messageIcon);
        MsgBox.ShowDialog();
        return Button_id;
    }

    /// <summary>
    /// We can use this method to add image on message box.
    /// I had taken all images in ImageList control so that
    /// I can easily add images. Image is displayed in 
    /// PictureBox control.
    /// </summary>
    /// <param name="MessageIcon">Type of image to be displayed.</param>
    private void addIconImage(enumMessageIcon MessageIcon)
    {
        switch (MessageIcon)
        {
            case enumMessageIcon.Error:
                pictureBox1.Image = imageList1.Images["Error"];  //Error is key 
                //name in imagelist control which uniquely identified images 
                //in ImageList control.
                break;
            case enumMessageIcon.Information:
                pictureBox1.Image = imageList1.Images["Information"];
                break;
            case enumMessageIcon.Question:
                pictureBox1.Image = imageList1.Images["Question"];
                break;
            case enumMessageIcon.Warning:
                pictureBox1.Image = imageList1.Images["Warning"];
                break;
        }
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        Button_id = "Cancel";
        MsgBox.Dispose();
    }

    private void btnNo_Click(object sender, EventArgs e)
    {
        Button_id = "No";
        MsgBox.Dispose();
    }

    private void btnYes_Click(object sender, EventArgs e)
    {
        Button_id = "Yes";
        MsgBox.Dispose();
    }
}

#region constant defiend in form of enumration which is used in showMessage class.

internal enum enumMessageIcon
{
    Error,
    Warning,
    Information,
    Question,
}

internal enum enumMessageButton
{
    OK,
    YesNo,
    YesNoCancel,
    OKCancel
}

#endregion

}

ma​​in.cs

String customResult = CustomMsgBox.ShowBox("Save changes to " + this.Text.Substring(0, this.Text.Length - 1) + "?", enumMessageIcon.Question);

最佳答案

MessageBoxButtons枚举没有这样的选项,它包含以下成员

enter image description here

所以对您来说更好的选择是自定义消息框,为此您可以尝试 this , thisthis ,或者只是按照食谱操作,

  • 使用构造函数创建一个表单(让它为 frmMessage),该构造函数接受一个字符串值,该字符串值是您想要显示的消息,
  • 给出适当的标题文本,设为保存确认
  • 放置一个标签以在构造函数的标签中显示消息。
  • 放置三个按钮,为其指定名称和文本,
  • 禁用这两个(是/否),您的消息框已准备就绪

使用示例:

现在您需要创建此消息框的对象并按如下方式调用它们:

frmMessage frmMessageInstance = new frmMessage("Save changes to " + this.Text.Substring(0, this.Text.Length - 1) + "?");
frmMessageInstance.ShowDialog();

关于c# - Winform 消息框,如何禁用 C# 中的 YESNO 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41216598/

相关文章:

c# - 使用反射按声明顺序获取属性

c# - 从 C# 调用高阶 F# 函数

c# - 共享 ComboBox 数据源

C# - MessageBox - 资源和换行符中的消息本地化

c# - MessageBox 锁定 TouchInput

c# - 无法将 Json 反序列化为对象

c# - 需要有关 C# LINQ 列表聚合表达式的建议

c# - 我需要一种更好的方法来查询已安装的防病毒软件。 c# 中可能有类似的东西吗?

vb.net - 如何在排序时保留 DataGridView 的编程彩色背景

c# - 如果查询 = 到 0,则显示消息框