【问题标题】:Winform messagebox, How to disable YESNO options in C#Winform 消息框,如何在 C# 中禁用 YESNO 选项
【发布时间】:2017-05-04 03:37:38
【问题描述】:

我想在消息框中显示 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 个按钮 YES 、 No 和 Cancel 但单击访问权限应仅授予取消按钮。那可能吗?

编辑: 谢谢大家的回答。

我为我的问题找到了灵魂

我的自定义消息框代码,希望对大家有所帮助

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

【问题讨论】:

  • 只需制作自己的对话框,而不是使用内置对话框。
  • 你为什么不用按钮创建自己的消息框?
  • 我是 C# 新手。我不知道如何创建自己的消息框。
  • 您必须创建自定义对话框并对其进行管理。
  • 只要你用谷歌搜索它就很简单。遵循本指南:youtu.be/MkFE_pM7jOc

标签: c# winforms messagebox


【解决方案1】:

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

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

  • 使用构造函数创建一个表单(让它成为frmMessage),接受一个字符串值,即您要显示的消息,
  • 给一个合适的标题Text,让它成为Save confirmation
  • 放置一个标签以在构造函数的标签中显示消息。
  • 放置三个按钮,为它们提供名称和文本,
  • 禁用两个(是/否),您的消息框已准备就绪

用法示例:

现在你需要创建这个消息框的一个对象并像下面这样调用它们:

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

【讨论】:

  • 实际上我想要 YesNo 按钮,但我想禁用它的点击访问。我想向用户显示 3 个按钮 YES 、 No 和 Cancel 但单击访问权限应仅授予取消按钮。这可能吗?
  • 你能建议我如何将消息按钮设置为默认的代码,可以在这里找到:stackoverflow.com/questions/41235881/…
  • @kaviarasan 这个问题已经有了一个公认的答案,对吧?那你还需要什么?
  • 我想将 Ok 按钮设为默认值。由于按钮是在运行时创建的,如何将特定按钮设为默认?
【解决方案2】:

正如Un-lucky 所解释的,您可以做到这一点的唯一方法是创建您自己的自定义messageBox。我会做这样的事情

/// <summary>
/// The form internally used by <see cref="CustomMessageBox"/> class.
/// </summary>
internal partial class CustomMessageForm : Form
{
    /// <summary>
    /// This constructor is required for designer support.
    /// </summary>
    public CustomMessageForm ()
    {
        InitializeComponent(); 
    } 

    public CustomMessageForm (string title, string description)
    {
        InitializeComponent(); 

        this.titleLabel.Text = title;
        this.descriptionLabel.Text = description;
    } 
}

/// <summary>
/// Your custom message box helper.
/// </summary>
public static class CustomMessageBox
{
    public static void Show (string title, string description)
    {
        // using construct ensures the resources are freed when form is closed
        using (var form = new CustomMessageForm (title, description)) {
            form.ShowDialog ();
        }
    }
}

Dan Abramov 的问题How to create a custom MessageBox? 的答案中慷慨地复制了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-12
    • 2017-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多