问题
How do I loop through without writing a million if statements? Here's a sample of the code I have it wrong as it will only check each box and then move on to the next. Forgot to say that all ten check boxes are in a panel and they need to be check in any combination. I don't want to write heaps of && or || I cant even count the combinations Please help me.
if (cbxTitle.Checked == true)
{
searched = "title";
}
else if (cbxAuthor.Checked == true)
{
searched = "author";
}
else if (cbxYear.Checked == true)
{
searched = "year";
}
else if (cbxWeight.Checked == true)
{
searched = "weight";
}
回答1:
It seems you want to include all search conditions selected by the user. If you didn't, then I'd definitely use radio buttons.
To avoid endless if
/else
statements, I'd use a foreach
loop on all CheckBox
controls, and--assuming you've been consistent in your variable names--you could then use a substring of the name to be appended to your search criteria.
So your algorithm would be more or less:
- get a list of all controls of type
Checkbox
- for each control found, if it is
Checked
, extract its name - obtain the search criterion represented by this control by removing the 'cbx' prefix
- append this criterion to your 'searched' string
EDIT:
At your request, I'm adding a sample.
Since it's a search application, I thought you would have a "Search Options" form.
Here's the section that initializes this form.
namespace SearchApplication
{
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
public partial class SearchOptionsForm : Form
{
// prefix you used for all your checkboxes
private const string prefix = @"cbx";
// store *all* checkboxes in your form
// if you had other checkboxes in your form,
// you would need to think how you would want to differentiate them
private IEnumerable<CheckBox> searchOptionControls;
// represents all choices made by the user to customize the search type
private string searched;
// since your 'searched' variable is a string,
// you need a filter separator to be able to tell user selections apart
private const string optionSeparator = @";";
public SearchOptionsForm()
{
this.InitializeComponent();
// initialize your collection of checkboxes
this.searchOptionControls = this.Controls.OfType<CheckBox>();
}
}
}
And here's the code that reacts to the user input, after she clicks "Apply"
namespace SearchApplication
{
using System;
using System.Windows.Forms;
public partial class SearchOptionsForm : Form
{
/// <summary>
/// Updates the 'searched' string with the selections made by the user.
/// </summary>
/// <param name="sender">The 'Apply' button.</param>
/// <param name="e">Not used in this implementation.</param>
private void ApplyClicked(object sender, EventArgs e)
{
// reset your filter after every click
this.searched = string.Empty;
// inspect all controls of interest
foreach (var currentOption in this.searchOptionControls)
{
// determine if user wants to use this filter
if (currentOption.Checked)
{
// append to your existing search options
searched += currentOption.Name.Substring(prefix.Length);
// include the filter separator
searched += optionSeparator;
}
}
// note that filter order in the string doesn't match display order
this.textBoxSearchFilters.Text = searched;
}
}
}
回答2:
You can replace your if statements with:
searched = this.Controls
.OfType<CheckBox>()
.First(x => x.Checked).Name.Substring(3).ToLower();
This assumes three things:
- You have at least one checked
CheckBox
- Your checkboxes are direct child element of your
Form
, they are not inside of a Panel or some other container - Name of your checkbox (without cbx prefix) is the value that you want assign to
searched
.
Also don't forget to include System.Linq
namespace with a using directive in order to use LINQ
methods (OfType
and First
)
回答3:
You must subscribe on the CheckedChange event of each checkbox.
When a checkbox is checked, take action for your search.
来源:https://stackoverflow.com/questions/23900873/10-check-boxes-multiple-combinations-in-c-sharp