C# – How to show a MessageBox with a checkbox

ccheckboxmessagebox

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?

Best Answer

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