How to show a MessageBox with a checkbox?
Asked Answered
E

4

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?

Electrolyte answered 30/6, 2013 at 0:17 Comment(2)
Create a custom dialog.Psi
Yep, I agree. Custom dialog is the way to go. See here as well: social.msdn.microsoft.com/Forums/vstudio/en-US/…Persecution
R
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;
}
Rodgers answered 30/6, 2013 at 0:32 Comment(2)
Thanks for the answer! It was a hard choice between yours and Tim's answer, but yours ended up being a bit easier visually to design.Electrolyte
The best solution yet!Adamina
P
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?");
Psi answered 30/6, 2013 at 0:26 Comment(2)
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? @TimPyosis
@Pyosis use Tim's answer as an example, and add more checkboxes in code.Giveandtake
R
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;
}
Rodgers answered 30/6, 2013 at 0:32 Comment(2)
Thanks for the answer! It was a hard choice between yours and Tim's answer, but yours ended up being a bit easier visually to design.Electrolyte
The best solution yet!Adamina
T
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
Tachometer answered 16/2, 2022 at 13:20 Comment(0)
T
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;
}
Tenedos answered 14/4, 2022 at 4:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.