Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
I have checkedListBox1 control. I want to write into a string "checked3487", ONLY the checked checkboxes text. (the text near the checkbox).
When I press one checkbox, "The check state is not updated until after the ItemCheck event occurs", but the "if (e.NewValue == CheckState.Checked)" is updating in realtime the check state, but only for the Checked state.
When I UNCHECK, it is updating after 2 click of the mouse, (after the ItemCheck event occurs).
How to uncheck and write the string properly?

Ive searched on google, I done 10 different variants of coding, and im desperate. What I miss?


C#
string checked3487 = ""; List<string> checkedList = new List<string>();
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
      {
          listBox1.Items.Clear(); checked3487 = ""; checkedList.Clear();

          foreach (var item in checkedListBox1.CheckedItems)
              checkedList.Add(item.ToString());

          if (e.NewValue == CheckState.Checked)
          {
              checkedList.Add(checkedListBox1.Items[e.Index].ToString());
          }
          if (e.NewValue == CheckState.Unchecked)
          {
              //?????????????????????????????????????????????????????????????????????/
          }



          foreach (string item in checkedList)
          {
              checked3487 += item;
          }
       }
Posted
Updated 6-Jun-15 12:40pm
v2

1 solution

Now is working!
it was a string manipulation after all. Damn it.
I got inspired and I find the solution.
here it is:
C#
if (e.NewValue == CheckState.Unchecked)
{
    if (checkedList.Contains(checkedListBox1.Items[e.Index].ToString()))
    {
        int i = checkedList.IndexOf(checkedListBox1.Items[e.Index].ToString());
        checkedList.RemoveAt(i);
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900