Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show a MessageBox with a checkbox?

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?

like image 823
TK421 Avatar asked Jun 30 '13 00:06

TK421


People also ask

How do I display message box?

To display a message box, call the static method MessageBox. Show. The title, message, buttons, and icons displayed in the message box are determined by parameters that you pass to this method.

Is MessageBox show a modal dialog window?

A message box is a modal dialog, which means no input (keyboard or mouse click) can occur except to objects on the modal form. The program must hide or close a modal form (typically in response to some user action) before input to another form can occur.

What is MessageBox show in C #?

MessageBox is a class in C# and Show is a method that displays a message in a small window in the center of the Form. MessageBox is used to provide confirmations of a task being done or to provide warnings before a task is done.


2 Answers

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?");
like image 78
Tim Schmelter Avatar answered Oct 27 '22 05:10

Tim Schmelter


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;
}
like image 23
Michael Petrotta Avatar answered Oct 27 '22 07:10

Michael Petrotta