0

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();
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
umb_dev
  • 1
  • 2
  • Quite hard to imagine the case you encountered. Any (1) sample actions you took, (2) your expected behavior, and (3) your actual (undesired) behavior? In image or text to reprent image will do. – Ian Feb 05 '16 at 02:27
  • I am loading a checklistbox with some people names. Then when I click the checkbox corresponding a name I want in the the team and I want it to appear in the other checklistbox, in order I can then check on it who's the team leader. In this case when I click on the first name, none is loaded into the the other. When I clck on a second name, I find only the first in the other box. If I uncheck one of the selected names, the other box is well populated, but if I check it again, the last one on the other box disappears. I guess is something wrong with refreshing, maybe I read data before refresh. – umb_dev Feb 05 '16 at 08:34
  • I don't what did it happened, but this morning I enlarged a bit both two checklistboxes and now it works fine. – umb_dev Feb 05 '16 at 08:42
  • IT should be something related to refresh, since if I click it slowly it gives me the right results, while clicking fastly returns strange combinations. I saw a lot of people advise not to use this component. – umb_dev Feb 05 '16 at 09:32
  • Problem is that on ItemCheck the clicked item is not changed yet. See http://stackoverflow.com/questions/3666682/which-checkedlistbox-event-triggers-after-a-item-is-checked – Antonín Lejsek Feb 05 '16 at 13:48
  • Thank you Antonin. I didn't find much infomation about this control. I felt sure there was something able to intercept value changes immediately since it appears when I click on an element, but I didn't know which was. – umb_dev Feb 07 '16 at 09:19

0 Answers0