[RESOLVED] Help with Checkedlistbox
What event is it that triggers when ever actually check a object in checkedlistbox
I have Itemscheck but it doesnt update textbox1 untill after i click something else.
c# Code:
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
textBox1.Text = querylist(checkedListBox1,"OR");
}
private string querylist (CheckedListBox CB_list,string and_or)
{
string querylist = "";
foreach (string x in CB_list.CheckedItems)
{
querylist += x + " " +and_or+" ";
}
return querylist;
}
Re: Help with Checkedlistbox
I believe you are looking for the SelectedIndexChanged event.
c# Code:
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (string s in this.checkedListBox1.CheckedItems)
{
// Do your thing.
}
}
Re: Help with Checkedlistbox
that seems to do it
what do you think could be done to eliminate the final OR off the string?
Re: Help with Checkedlistbox
I guess before you return it you could remove the last "OR" with the String.Substring method.
c# Code:
if (querylist != string.Empty)
{
querylist = querylist.Substring(0, querylist.LastIndexOf(" OR "));
}
Re: Help with Checkedlistbox
SelectedIndexChanged is NOT what you want. Selecting an item and checking an item are two completely different things. Selecting an item is exactly the same as it is in a regular ListBox. Selected items are highlighted in blue by default. Checking an item means ticking the box beside it. An item can be checked without being selected, or it can be selected without being checked, or it can be neither or it can be both. That's why the control has a SelectedItems collection and a CheckedItems collection: because they are not the same thing. The SelectedIndexChanged event is raised when the SelectedIndex property value changes, which may coincide with an item being checked but will not always as it has no specific connection.
This is just another example of why you need to read the relevant documentation, especially when things don't work the way you expect. Had you done so you'd have seen this:
Quote:
The check state is not updated until after the ItemCheck event occurs.
which means that the CheckedItems collection will not yet have changed when you're looking at it. More reading of the documentation would have revealed that the event provides an ItemCheckEventArgs object, which has CurrentValue and NewValue properties. If the CurrentValue is true and the NewValue is false then you know the item is being unchecked. The opposite is true if the item is being checked. All the information you need is at your fingertips in the MSDN library, but you'll never see it if you don't look.
Re: Help with Checkedlistbox