Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I'm creating a custom message box in winforms, with a checkbox - but I'm not sure how I get the state of the checkbox when the user closes the form.

I use this to call the MsgBox:
C#
public static MsgBoxResult Show(string message, string caption, MsgBoxButtons msgBoxButtons, MsgBoxIcon icon, MsgBoxDefaultButton defaultButton, 
            MsgBoxCheckBox donotshowagain)
        {
            return new MsgBoxForm(message, caption, msgBoxButtons, icon, defaultButton, donotshowagain).Show();
        }


Where donnotshowagain is the checkbox parameter, I have created an enum MsgBoxCheckBox that contains:
C#
[Flags]
    public enum MsgBoxCheckBox
    {
        /// <summary>
        /// Show the check box.
        /// </summary>
        Show = 1,
        /// <summary>
        /// Hides the check box (default)
        /// </summary>
        Hide = 0,
        /// <summary>
        /// Show as checked.
        /// </summary>
        Checked = 2,
    }


I hope that someone can point me in the right direction with this.

I added my code to Git, forgot that with pastbin, the enums wasn't included.
Also I finished up my code, so now it should be usable, also with the check box state.
Code: GitHub - Lupu5R3x/MsgBox: A Custom message box[^]
/LR

What I have tried:

Looked for answers on the web, also I looked at the code from other custom message box's that have a checkbox - but the only one I found that uses a "show class" that calls the form class is InformationBox, and there the out keyword is used:
C#
public static InformationBoxResult Show(string text, out CheckState checkBoxState, params object[] parameters)
        {
            return new InformationBoxForm(text, parameters).Show(out checkBoxState);
        }

But the code it self is way to complicated for a newbe like me to figure out.
I did try to add the out to my own code, but got a bunch of errors.
Posted
Updated 29-Feb-20 11:59am
v6
Comments
Richard MacCutchan 29-Feb-20 10:28am    
The way you are calling it, all context is lost after the return. You should create an instance of the form, call Show, or better still, ShowDialog. When it returns you still have the instance object, so you can check the status of the checkbox, which is a property of the Form.
Lupu5R3x 29-Feb-20 10:43am    
In my form code I do call ShowDialog (maybe I should had added that information :()

In my form code I have internal new MsgBoxResult Show()
{
SetIcons();
SetButtons();
SetSize();
SetButtonsLayout();
SetDefaultButton();
SetOrder();
PlaySound();
SetCheckBox();
SetColor();
ShowDialog();
return Result;
}

1 solution

After rereading the informationbox code, I figured out how to get the state of my checkbox.

I had to create a new MsgBoxResult without the new keyword, and add the out parameter to that show.
So I ended up with this:
internal MsgBoxResult Show(out CheckState state)
       {
           Result = Show();
           state = chkDoNotShowAgain.CheckState;
           return Result;
       }

And in the caller:
public static MsgBoxResult Show(string message, string caption, MsgBoxButtons msgBoxButtons, MsgBoxIcon icon, MsgBoxDefaultButton defaultButton,
           MsgBoxCheckBox doNotShowAgain, out System.Windows.Forms.CheckState checkBoxState)
       {
           return new MsgBoxForm(message, caption, msgBoxButtons, icon, defaultButton, doNotShowAgain).Show(out checkBoxState);
       }


Then I can call my MsgBox like this:
CheckState doNotShowAgain = CheckState.Indeterminate;
            MsgBoxResult result = MsgBox.Show("Message", "Caption", MsgBoxButtons.OK, MsgBoxIcon.None, MsgBoxDefaultButton.Button1, MsgBoxCheckBox.Show, out doNotShowAgain);
 
Share this answer
 
Comments
Richard MacCutchan 1-Mar-20 3:03am    
Exactly what I said in my comment.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900