Initially, I had the following:
[Flags]
public enum QueryFlag
{
None = 0x00,
CustomerID = 0x01,
SalesPerson = 0x02,
OrderDate = 0x04
}
When the checkboxes are checked or not checked, I would add / remove flags from:
QueryFlag qflag;
My idea is that when the user clicks the Search button, I will iterate over the actual flags set in qflagto change the sentence .Wherein my LINQ to Sql. However, it Enum.GetValues(qflag.GetType())returns all the values of QueryFlag itself. Not healthy.
My decision:
class myForm : Form
{
List<QueryFlag> qflag = new List<QueryFlag>();
private void chkOrderDate_CheckedChanged(object sender, EventArgs e)
{
if (chkOrderDate.Checked && !qflags.Contains(QueryFlag.OrderDate))
qflags.Add(QueryFlag.OrderDate);
else
qflags.Remove(QueryFlag.OrderDate);
}
private void cmdSearch_Click(object sender, EventArgs e)
{
if (qflags.Count == 0)
{
rtfLog.AppendText("\nNo search criteria selected.");
return;
}
foreach (QueryFlag flag in qflag)
{
rtfLog.AppendText(string.Format("\nSearching {0}", flag.ToString()));
}
}
}
public enum QueryFlag
{
CustomerID,
SalesPerson,
OrderDate
}
I have 3 checkboxes and this works without any problems. But I am wondering if there is a better way to accomplish this iteration.
source
share