Results 1 to 6 of 6

Thread: [RESOLVED] Help with Checkedlistbox

  1. #1

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Resolved [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:
    1. private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    2.         {
    3.            
    4.             textBox1.Text = querylist(checkedListBox1,"OR");
    5.         }
    6.  
    7.         private string querylist (CheckedListBox CB_list,string and_or)
    8.          {
    9.              string querylist = "";
    10.              foreach (string x in CB_list.CheckedItems)
    11.              {
    12.                  querylist += x + " " +and_or+" ";
    13.              }
    14.              return querylist;
    15.         }

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Help with Checkedlistbox

    I believe you are looking for the SelectedIndexChanged event.

    c# Code:
    1. private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
    2. {
    3.     foreach (string s in this.checkedListBox1.CheckedItems)
    4.     {
    5.         // Do your thing.                
    6.     }
    7. }

  3. #3

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: Help with Checkedlistbox

    that seems to do it


    what do you think could be done to eliminate the final OR off the string?

  4. #4
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Help with Checkedlistbox

    I guess before you return it you could remove the last "OR" with the String.Substring method.

    c# Code:
    1. if (querylist != string.Empty)
    2. {
    3.     querylist = querylist.Substring(0, querylist.LastIndexOf(" OR "));
    4. }

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: Help with Checkedlistbox

    Thanks guys

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width