17

I would like to create a MessageBox that has Yes/No buttons AND a checkbox.

The application is a picture resizer and it will be re-sizing a number of pictures at once; in the process it will check if the new location filename exists with the option to overwrite it.

The MessageBox will give the user the option to overwrite any new files if desired, while the checkbox will prevent having to click Yes x number of times if they want to overwrite every file.

How do I add a checkbox to a MessageBox dialog?

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
TK421
  • 801
  • 5
  • 16
  • 24
  • 5
    Create a custom dialog. – Tim Schmelter Jun 30 '13 at 00:18
  • Yep, I agree. Custom dialog is the way to go. See here as well: http://social.msdn.microsoft.com/Forums/vstudio/en-US/2fa7e3e3-f55f-4c0d-bcf5-c7278af1d730/how-to-display-a-checkbox-in-message-box – rsbarro Jun 30 '13 at 00:19

4 Answers4

20

Create a custom dialog. Here is something that could give you an idea:

public static class CheckboxDialog
{   
    public static bool ShowDialog(string text, string caption)
    {
        Form prompt = new Form();
        prompt.Width = 180;
        prompt.Height = 100;
        prompt.Text = caption;
        FlowLayoutPanel panel = new FlowLayoutPanel();
        CheckBox chk = new CheckBox();
        chk.Text = text;
        Button ok = new Button() { Text = "Yes" };
        ok.Click += (sender, e) => { prompt.Close(); };
        Button no = new Button() { Text = "No" };
        no.Click += (sender, e) => { prompt.Close(); };
        panel.Controls.Add(chk);
        panel.SetFlowBreak(chk, true);
        panel.Controls.Add(ok);
        panel.Controls.Add(no);
        prompt.Controls.Add(panel);
        prompt.ShowDialog();
        return chk.Checked;
    }
}

You can use it in this way:

bool overwrite = CheckboxDialog.ShowDialog("overwrite", "Overwrite location?");
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • What if we have multiple checkboxes? say chk1, chk2, chk3 and want to return the values of the checkboxes which are checked, we can have 2 checkboxes at a time and return both the values. And then how we'll use that? @Tim – silent_programmer Aug 14 '15 at 23:29
  • @silent_programmer use Tim's answer as an example, and add more checkboxes in code. – Todd Price Sep 12 '16 at 22:01
18

You can't add a checkbox to a MessageBox. As Tim and rsbarro suggest, you should create a custom dialog. Tim's answer will work well, and doesn't require creation of a new class. If you want to design the form in the designer though, you could try this.

  • Create a new form with the two buttons and the checkbox you'll need.
  • In the forms designer, set the DialogResult property of the Yes button to Yes, and that of the No button to No. This'll let you discover what button the user clicked.
  • Create a property on the form that reflects the state of the checkbox (optional - I don't like to reference a control on one form from another form, but if you make the checkbox public, that'll work too).
public bool DoForAll
{
    get { return checkBox.Checked; }
}
  • On your main form, show the child form when needed. For instance:
var options = new Options();
var result = options.ShowDialog();
if (result == DialogResult.Yes)
{
    var doForAll = options.DoForAll;
}
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
0

For vb.net (view is this code, only show one buttton)

clsMsgBoxV1.ShowDialog("PDF solo de albaranes. (No avisarme mas)", "PDF")

and use this shared class:

Public Class clsMsgBoxV1

    Shared prompt As Form
    Shared chk As CheckBox
    Public Shared Function ShowDialog(Text As String, caption As String) As Boolean
        'frmUsuario


        prompt = New Form()
        chk = New CheckBox()

        prompt.StartPosition = FormStartPosition.CenterParent
        prompt.Width = 220
        prompt.Height = 150
        prompt.Text = caption

        prompt.MinimumSize = prompt.Size
        prompt.MaximumSize = prompt.Size

        prompt.MaximizeBox = False
        prompt.MinimizeBox = False

        Dim panel As FlowLayoutPanel =   New FlowLayoutPanel()

        chk.Text = Text
        chk.Width = chk.Width * 2
        chk.Height = 50
        Dim ok As Button = New Button() With {
            .Text = "Leido"
        }
        AddHandler ok.Click, AddressOf OKClick


        'Dim no As Button = New Button() With {
        '.Text = "No"
        '}
        'AddHandler no.Click, AddressOf NoClick


        ok.Width = prompt.Width - 30
        panel.Controls.Add(chk)
        panel.SetFlowBreak(chk, True)
        panel.Controls.Add(ok)
        'panel.Controls.Add(no)
        prompt.Controls.Add(panel)
        prompt.ShowDialog()
        prompt.Dispose()
        Return chk.Checked
    End Function

    Private Shared Sub NoClick(sender As Object, e As EventArgs)
        prompt.Close()
    End Sub
    Private Shared Sub OKClick(sender As Object, e As EventArgs)
        prompt.Close()

    End Sub

End Class
R.Alonso
  • 989
  • 1
  • 8
  • 9
0

I realize this is a very old thread, but I just wanted to give my two cents.

Ookii is the simplest way to implement a CheckBox into a MessageBox, or customize it: https://github.com/ookii-dialogs/ookii-dialogs-wpf (I was using it because of the classic "Folder Browser" dialog, so thought might as well use it for the checkbox thing)

Here's my code:

using Ookii.Dialogs.Wpf;

//create instance of ookii dialog
TaskDialog dialog = new();

//create instance of buttons
TaskDialogButton butYes = new TaskDialogButton("Yes");
TaskDialogButton butNo = new TaskDialogButton("No");
TaskDialogButton butCancel = new TaskDialogButton("Cancel");

//checkbox 
dialog.VerificationText = "Dont Show Again"; //<--- this is what you want.

//customize the window
dialog.WindowTitle = "Confirm Action";
dialog.Content = "You sure you want to close?";
dialog.MainIcon = TaskDialogIcon.Warning;

//add buttons to the window
dialog.Buttons.Add(butYes);
dialog.Buttons.Add(butNo);
dialog.Buttons.Add(butCancel);

//show window
TaskDialogButton result = dialog.ShowDialog(this);

//get checkbox result
if (dialog.IsVerificationChecked)
{
    //do stuff
}

//get window result
if (result != butYes)
{
    //if user didn't click "Yes", then cancel the closing.
    e.Cancel = true;
    return;
}