Winform messagebox, How to disable YESNO options in C#

3.5k views Asked by At

I want to show YesNoCancel button in the message box, but at the same time, I wanted to disable YesNo button and enable only Cancel button.

The reason I wanted to do like this is I am doing a demo application where I want to show users that particular feature is available but at the same time I don't want to give them save access.

Following is my code, now to how to Disable YesNo button.

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

Actually I want to show YesNo buttons but I wanted to disable click acess for it. I wanted to show users 3 buttons YES , No and Cancel but click acess should be given only to cancel button. Is that possible?

edit: Thanks all for the answers.

I have found soultion for my question

My code for custom message box, I hope this might help someone

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

There are 2 answers

4
sujith karivelil On BEST ANSWER

The MessageBoxButtons Enumerations does not have such options, It contains the following members

enter image description here

So the better option for you is a custom message box, for this you can try this, this or this, Or simply follow the recipe,

  • Create a Form(let it be frmMessage) with a constructor accepts a string value that is the message that you wanted to display,
  • Give an appropriate title Text, let it be Save confirmation,
  • Place a Label to display the message in the label from the constructor.
  • Place Three buttons, Give name and Text for them,
  • Disable the Two(Yes/No), Your message box is ready

Usage Example:

Now you need to create an object of this message box and call them like the following:

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

As explained by Un-lucky, the only way you can do that is by creating your own custom messageBox. I would do something like this

/// <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 ();
        }
    }
}

Graciously copied from an answer by Dan Abramov for question How to create a custom MessageBox?