Click here to Skip to main content
15,888,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 3 check boxes that i want to validate at least one is checked when i click the print button. when I click print it prompts me to select at least one check box when I say ok to the message it prints anyway.How can i stop that?


<pre>private void btnPrint_Click(object sender, EventArgs e)
        {

            try
            {

                if (checkBox1.Checked)
                    if (checkBox2.Checked)
                if (checkBox3.Checked)
                    {

                    }
                if (!checkBox1.Checked && !checkBox2.Checked && !checkBox3.Checked)
                {
                    MessageBox.Show("Please select at least one!");
                }
            }


            catch (Exception ex)
            {

                printDocument1.PrintPage += printDocument1_PrintPage;
                printDocument1.Print();
            }
        }


What I have tried:

Related entries and internet search
Posted
Updated 9-Aug-17 5:38am

1 solution

Why don't you disable the Button until one of the checkboxes are checked?
C#
private void CheckChanged(object sender, EventArgs e)
{
    button1.Enabled = checkBox1.Checked || checkBox2.Checked || checkBox3.Checked;
}
 
Share this answer
 
Comments
Member 12349103 9-Aug-17 12:35pm    
How would i use that in the above code?
Graeme_Grant 9-Aug-17 12:50pm    
point all 3 checkbox CheckChanged events to the above method and set the Button.IsEnabled state to false. When one of the checks are ticked, the button is enabled, if all are then unticked, the button is disabled.
Member 12349103 9-Aug-17 16:19pm    
Thanks that did the trick
Graeme_Grant 9-Aug-17 17:07pm    
Glad to hear! :)

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