I got a couple of if statements. What I want is, as soon as one condition is met, I should get out of the method. I will be using this if statements on filtering data for a datagridview or report. With my code below, it doesn't do this. It seems to go through all conditions. As soon as it finds a condition that meets it, the program executes it and proceeds to another if statement and if it sees that it meets the condition also, it too gets executed. I think this is pretty basic for you. I don't remember or know how I should do this.
private void btnRoute_Click(object sender, EventArgs e)
{
if (cbWithRoute.Checked)
{
// StartSearch(txtRoute.SelectedValue.ToString());
MessageBox.Show(@"route");
}
if (cbWithRoute.Checked && cbWithWholeSeller.Checked)
{
//StartSearch(txtRoute.SelectedValue.ToString(), txtWholeSeller.SelectedValue.ToString());
MessageBox.Show(@"route wholeseller");
}
if (cbWithRoute.Checked && cbWithCustomer.Checked)
{
MessageBox.Show(@"route customer");
}
if (cbWithRoute.Checked && cbWithWholeSeller.Checked && cbWithDate.Checked)
{
//StartSearch(txtRoute.SelectedValue.ToString(),
// txtWholeSeller.SelectedValue.ToString(), Convert.ToDateTime(txtFromDate.Text).ToShortDateString(),
// Convert.ToDateTime(txtToDate.Text).ToShortDateString());
MessageBox.Show(@"route wholseller date");
}
if (cbWithRoute.Checked && cbWithCustomer.Checked && cbWithDate.Checked)
{
MessageBox.Show(@"route date customer");
}
if (cbWithRoute.Checked && cbWithWholeSeller.Checked && cbWithDate.Checked && cbWithCustomer.Checked)
{
//StartSearch(txtRoute.SelectedValue.ToString(),
// txtWholeSeller.SelectedValue.ToString(), Convert.ToDateTime(txtFromDate.Text).ToShortDateString(),
// Convert.ToDateTime(txtToDate.Text).ToShortDateString(), txtCustomer.SelectedValue.ToString());
MessageBox.Show(@"route wholeseller date customer");
}
//else
//{
// MessageBox.Show(@"Check criteria to search.");
//}
}
I commented the true code and replaced it with message boxes to show me what checkboxes are checked. Thank you.
Since your method retrun type is void
add a return
to end of all the if conditions like below;
if (cbWithRoute.Checked)
{
// StartSearch(txtRoute.SelectedValue.ToString());
MessageBox.Show(@"route");
return;
}
if (cbWithRoute.Checked && cbWithWholeSeller.Checked)
{
//StartSearch(txtRoute.SelectedValue.ToString(), txtWholeSeller.SelectedValue.ToString());
MessageBox.Show(@"route wholeseller");
return;
}
and so on....
Where ever you want it to stop executing, place the following line
return;
I wont assume to understand the business logic behind the checks but you may also want to read up on else
statements and else if
Else-if is the concept you need. Like below.
if (cbWithRoute.Checked)
{
// StartSearch(txtRoute.SelectedValue.ToString());
MessageBox.Show(@"route");
}
else if (cbWithRoute.Checked && cbWithWholeSeller.Checked)
{
//StartSearch(txtRoute.SelectedValue.ToString(), txtWholeSeller.SelectedValue.ToString());
MessageBox.Show(@"route wholeseller");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.