Winform消息框,如何在C#中禁用YESNO选项

时间:2016-12-19 06:02:53

标签: 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按钮,但我想禁用它的Click acess。我想向用户显示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

}

main.cs

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

2 个答案:

答案 0 :(得分:3)

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

enter image description here

因此,更好的选择是自定义消息框,为此您可以尝试thisthisthis,或者只需按照食谱,

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

用法示例:

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

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

答案 1 :(得分:0)

正如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?

的回答慷慨地复制