I built a small WinForm application that has checkboxes to filters users with different properties.
Each user has a name, color and number.
With the program you can filter by color or name the users that you will see:
Inside the application there is a static list of users:
// SomeUser(<name>, <color>, <number>);
SomeUser("user1", "red", "one");
SomeUser("user2", "blue", "one");
SomeUser("user3", "red", "two");
SomeUser("user4", "blue", "two");
SomeUser("user4", "yellow", "two");
SomeUser("user4", "blue", "three");
The application works as expected.
If you are not specified any filters, it will show you all.
There is OR inside each groupbox.
There is AND bettwen the groupboxes.
Meaning:
If you checked red
and blue
, it will search for users that have red color OR blue color.
If you checked red
, blue
and one
, it will search for users that have red OR blue color AND one
number.
I hope you understand from these two small examples.
This is the filter function:
void countFinding(CheckBox checkbox, string userField, ref int found)
{
if (checkbox.Checked)
{
if (userField == checkbox.Text)
{
found++;
}
}
}
private bool isFilter(SomeUser user)
{
bool isAllowed = false;
int shouldBeFound = 0;
int found = 0;
if(this.checkBoxRed.Checked || this.checkBoxBlue.Checked)
{
shouldBeFound++;
countFinding(this.checkBoxRed, user.color, ref found);
countFinding(this.checkBoxBlue, user.color, ref found);
}
if (this.checkBoxOne.Checked || this.checkBoxTwo.Checked)
{
shouldBeFound++;
countFinding(this.checkBoxOne, user.number, ref found);
countFinding(this.checkBoxTwo, user.number, ref found);
}
if (shouldBeFound == found)
{
isAllowed = true;
}
return isAllowed;
}
Inside the isFilter()
function I am checking twice the same checkbox.
In the first if
I am checking:
if(this.checkBoxRed.Checked || this.checkBoxBlue.Checked)
and if one of them is true it goes (twice) to an inner function (countFinding()
)that also checks if the checkbox is checked.
So, it appear them I am checking at least one checkbox twice to see if it checked.
Is it possible to make it better? what do you think?