I've got a button on a Windows Form that starts off disabled (Enabled = false).
I want to enable it when the user has selected an item from a combobox, and checked at least one item from a CheckedListBox. The user can check/uncheck all the items in the CheckedListBox via a Checkbox, like so:
private void checkBoxAll_CheckedChanged(object sender, EventArgs e)
{
for (int x = 0; x < MemberListBox.Items.Count; x++)
{
MemberListBox.SetItemChecked(x, checkBoxAll.Checked);
}
GenPacketBtn.Enabled = MonthAndMemberSelected();
}
So, I thought this would be easy - just check that something has been selected in both the combobox and the CheckedListBox:
private bool MonthAndMemberSelected()
{
return ((comboBoxMonth.SelectedIndex >= 0) && (MemberListBox.SelectedIndex >= 0));
}
...and then enable or disable the button when these controls are changed, like so:
private void comboBoxMonth_SelectedIndexChanged(object sender, EventArgs e)
{
GenPacketBtn.Enabled = MonthAndMemberSelected();
}
private void MemberListBox_SelectedIndexChanged(object sender, EventArgs e)
{
GenPacketBtn.Enabled = MonthAndMemberSelected();
}
Since the checkbox holds so much sway over the CheckedListBox, I added that code to the checkBoxAll_CheckedChanged() event:
private void checkBoxAll_CheckedChanged(object sender, EventArgs e)
{
for (int x = 0; x < MemberListBox.Items.Count; x++)
{
MemberListBox.SetItemChecked(x, checkBoxAll.Checked);
}
GenPacketBtn.Enabled = MonthAndMemberSelected();
}
...and (since it didn't work, out of desperation) to its Clicked event, too:
private void checkBoxAll_Click(object sender, EventArgs e)
{
GenPacketBtn.Enabled = MonthAndMemberSelected();
}
But it doesn't work - enabling the button works, but once it has been enabled, it won't disable again if I uncheck all the items in the CheckedListBox. Why not?