I'm creating a form with two Checklistboxes. The first one is populated by a database query, while the latter is dinamically created when I check elements on the first one and these ones have to be replicated on the second one. The first works fine, while the second give me troubles, since it returns me always n-1 elements. I tried unsucessfully several methods, for instance BeginUpdate() and EndUpdate(), cblStaff..GetItemChecked(i)) instead of cblStaff.GetItemCheckState(i).Equals(CheckState.Checked).
I guess it's a matter of value refresh since I placed a label (lblCounter) to check how many items on cblStaff have been selected.
Maybe it's a matter of events, and I'm using the wrong one, but I'm getting crazy with this control.
This is the code:
private void cblStaff_ItemCheck(object sender, ItemCheckEventArgs e)
{
cblStaff.BeginUpdate();
switch (e.CurrentValue)
{
case CheckState.Indeterminate:
e.NewValue = CheckState.Checked;
break;
case CheckState.Checked:
e.NewValue = CheckState.Unchecked;
persons--;
break;
case CheckState.Unchecked:
e.NewValue = CheckState.Checked;
persons++;
break;
}
cblStaff.EndUpdate();
TeamUpdate();
}
private void RefreshDirector()
{
cblDirector.Items.Clear();
int counter = 0;
for (int i = 0; i < cblStaff.Items.Count; i++)
{
if (cblStaff.GetItemCheckState(i).Equals(CheckState.Checked)) {
cblDirector.Items.Add(cblStaff.Items[i].ToString(), CheckState.Unchecked);
counter++;
lblCounter.Text = "" + counter;
}
}
}
private void TeamUpdate()
{
switch (persons) {
case 0:
lblTeam.Text = "No team";
break;
case 1:
lblTeam.Text = "1 person team";
break;
default:
lblTeam.Text = "" + persons + " people team";
break;
}
cblStaff.Refresh();
RefreshDirector();
}