blah blah blah is here! blah blah » Close

up0down
link

if (chk1min.Checked == false && chk2min.Checked == false && chk5min.Checked == false)
{
MessageBox.Show("PLEASE AT LEAST CHOOSE A TIMING");

}
else if (chk1min.Checked == true && chk2min.Checked == true && chk5min.Checked == true
|| chk1min.Checked == true && chk5min.Checked == true
|| chk2min.Checked == true && chk5min.Checked == true
|| chk1min.Checked == true && chk2min.Checked == true)
{
MessageBox.Show(" PLEASE CHOOSE A TIMING ONLY ");

}


else if (chk1min.Checked == true)
{

timer1min.Start();
MessageBox.Show("START!");
btnStart.Enabled = false;
lblRandomNumber.Text = Convert.ToString(ResistorValue) + " " + "Ω" + "±" + " " + Convert.ToString(BandTolerance) + "" + "%";
chk1min.Enabled = false;
chk2min.Enabled = false;
chk5min.Enabled = false;


}

else if (chk5min.Checked == true)
{

timer5min.Start();
MessageBox.Show("START!");
btnStart.Enabled = false;
lblRandomNumber.Text = Convert.ToString(ResistorValue) + " " + "Ω" + "±" + " " + Convert.ToString(BandTolerance) + "" + "%";
chk1min.Enabled = false;
chk2min.Enabled = false;
chk5min.Enabled = false;


}


else if (chk2min.Checked == true)
{

timer2min.Start();
MessageBox.Show("START!");
btnStart.Enabled = false;
lblRandomNumber.Text = Convert.ToString(ResistorValue) + " " + "Ω" + "±" + " " + Convert.ToString(BandTolerance) + "" + "%";
chk1min.Enabled = false;
chk2min.Enabled = false;
chk5min.Enabled = false;

last answered one year ago

2 answers

up0down
link

No, you can only use switch with integers, characters and strings - not bools. Also you can only switch on the value of a single variable or expression, not on several.

You can simplify it a bit though. For:

* chk1min.Checked == true, you can simply do chk1min

and for:

* chk1min.Checked == false, you can simply do !chk1min

There's also some repeated code for the last 3 'else ifs' which you could eliminate by introducing a bool variable to indicate that a timer has been chosen.

So the whole thing could be rewritten as follows:

bool timerChosen = false;

if (!chk1min.Checked && !chk2min.Checked && !chk5min.Checked)
{
MessageBox.Show("PLEASE AT LEAST CHOOSE A TIMING");
}

else if (chk1min.Checked && chk2min.Checked && chk5min.Checked
|| chk1min.Checked && chk5min.Checked
|| chk2min.Checked && chk5min.Checked
|| chk1min.Checked && chk2min.Checked)
{
MessageBox.Show(" PLEASE CHOOSE A TIMING ONLY ");
}

else if (chk1min.Checked)
{
timerChosen = true;
timer1min.Start();
}

else if (chk5min.Checked)
{
timerChosen = true;
timer5min.Start();
}

else if (chk2min.Checked)
{
timerChosen = true;
timer2min.Start();
}

if (timerChosen)
{
MessageBox.Show("START!");
btnStart.Enabled = false;
lblRandomNumber.Text = Convert.ToString(ResistorValue) + " " + "Ω" + "±" + " " + Convert.ToString(BandTolerance) + "" + "%";
chk1min.Enabled = false;
chk2min.Enabled = false;
chk5min.Enabled = false;
}

up0down
link

or you could use radio buttons and have 1 event handler for all 3 check changed events:

private void HandleTimingsChanged( object sender, EventArgs e) {
if ( sender == chk1min) {
timerChosen = true;
timer1min.Start();
// whatever else
} else if ( sender == chk2min ) {
timerChosen = true;
timer2min.Start();
// whatever else
} else if (sender == chk5min ) {
timerChosen = true;
timer5min.Start();
// whatever else
}
}

Feedback