Results 1 to 10 of 10

Thread: CheckedListBox Help

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2003
    Posts
    42

    CheckedListBox Help

    Hi, guys I am new to .NET and programming in it self. SuperN00b you can call me. Anyway this is my questions. I have a form and on that form I have a CheckedListBox and it looks something like this...

    All
    Strawberry
    Apple
    Kiwi
    Orange
    Banana
    Grape

    Now the user can click on the indivdual fruit with no problem but what want to happen is that with the user clicks on the "All" checkbox I would like all of the items below to automatically be checked. Any idea how I would accomplish this ?


    Thanks In Advance
    ScarEye

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: CheckedListBox Help

    VB Code:
    1. Private m_blnInLoop As Boolean
    2.     Private Sub ListView1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles ListView1.ItemCheck
    3.         Dim li As ListViewItem
    4.  
    5.         If Not m_blnInLoop Then
    6.             m_blnInLoop = True
    7.             If ListView1.Items(e.Index).Text = "All" And e.NewValue = CheckState.Checked Then
    8.  
    9.                 Dim liSelect As ListViewItem
    10.                 For Each liSelect In ListView1.Items
    11.                     liSelect.Checked = True
    12.                 Next
    13.  
    14.  
    15.             End If
    16.             m_blnInLoop = False
    17.         End If
    18.  
    19.     End Sub

  3. #3
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Re: CheckedListBox Help

    Try:
    VB Code:
    1. Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
    2.       If CheckedListBox1.SelectedItem.ToString() = "All" And e.NewValue = CheckState.Checked Then
    3.          Dim index As Integer
    4.          For Each index In CheckedListBox1.CheckedIndices
    5.             CheckedListBox1.SetItemChecked(index, False)
    6.          Next
    7.       ElseIf e.NewValue = CheckState.Checked Then
    8.          CheckedListBox1.SetItemChecked(CheckedListBox1.Items.IndexOf("All"), False)
    9.       End If
    10.    End Sub

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2003
    Posts
    42

    Re: CheckedListBox Help

    Quote Originally Posted by Aaron Young
    Try:
    VB Code:
    1. Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
    2.       If CheckedListBox1.SelectedItem.ToString() = "All" And e.NewValue = CheckState.Checked Then
    3.          Dim index As Integer
    4.          For Each index In CheckedListBox1.CheckedIndices
    5.             CheckedListBox1.SetItemChecked(index, False)
    6.          Next
    7.       ElseIf e.NewValue = CheckState.Checked Then
    8.          CheckedListBox1.SetItemChecked(CheckedListBox1.Items.IndexOf("All"), False)
    9.       End If
    10.    End Sub



    Aaron,


    Thanks for you repsonse, but this code does not do what I would like.

    What your code does is.

    All
    Strawberry
    Apple
    Orange
    Banana


    You can click on apple, strawberry, etc etc and when you finally click on "All"
    apple, strawberry, etc etc they all UNcheck and All is just checked.


    What I would like is when you click on "All" I would like Strawberry, Apple etc etc to BE checked. And when you UNcheck "All" I would like everyone below that UNCHECKED.

    Thank You
    ScarEye

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2003
    Posts
    42

    Re: CheckedListBox Help

    Quote Originally Posted by Negative0
    VB Code:
    1. Private m_blnInLoop As Boolean
    2.     Private Sub ListView1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles ListView1.ItemCheck
    3.         Dim li As ListViewItem
    4.  
    5.         If Not m_blnInLoop Then
    6.             m_blnInLoop = True
    7.             If ListView1.Items(e.Index).Text = "All" And e.NewValue = CheckState.Checked Then
    8.  
    9.                 Dim liSelect As ListViewItem
    10.                 For Each liSelect In ListView1.Items
    11.                     liSelect.Checked = True
    12.                 Next
    13.  
    14.  
    15.             End If
    16.             m_blnInLoop = False
    17.         End If
    18.  
    19.     End Sub

    Negative0,

    I tried your code, but I am not dealing with a Listbox. I am dealing with a CHECKEDLISTBOX. I tried your code and tried minipulating it for a checkedlistbox1 but I am not that good of a programmer yet to be able to do that.

    Read what I wrote to Aaron so you can understand.

    Thank you for your help also


    ScarEye

  6. #6
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Re: CheckedListBox Help

    Sorry about that, guess I should have read more closely...
    VB Code:
    1. Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
    2.       Static m_isUpdating As Boolean
    3.  
    4.       If Not m_isUpdating Then
    5.          m_isUpdating = True
    6.  
    7.          If CheckedListBox1.SelectedItem.ToString() = "All" Then
    8.             Dim index As Integer
    9.             For index = 1 To CheckedListBox1.Items.Count - 1
    10.                CheckedListBox1.SetItemChecked(index, (e.NewValue = CheckState.Checked))
    11.             Next
    12.          ElseIf e.NewValue = CheckState.Unchecked Then
    13.             CheckedListBox1.SetItemChecked(CheckedListBox1.Items.IndexOf("All"), False)
    14.          End If
    15.  
    16.          m_isUpdating = False
    17.       End If
    18.    End Sub

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2003
    Posts
    42

    Re: CheckedListBox Help

    Aaron,

    You are the fkn MAN !

    Thank you very much.

    ScarEye

  8. #8

    Thread Starter
    Member
    Join Date
    Jun 2003
    Posts
    42

    Re: CheckedListBox Help

    Aaron,

    If it's possible can you explain to me what each line of code is doing ?

    Just because I want to learn what is actually going on.

    I am a n00b at this and I want to learn.


    Thank you once again.

    ScarEye

  9. #9
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Re: CheckedListBox Help

    Sure:
    VB Code:
    1. Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
    2.       ' Create a static variable which retains it's value between
    3.       ' calls to this event.  We'll use this to know when we're
    4.       ' in the process of checking/unchecking boxes so as not to
    5.       ' cause an infinite loop (as this event is called when "any"
    6.       ' item is checked/unchecked, even through code), preventing the
    7.       ' dreaded Stack Overflow message.
    8.       Static m_isUpdating As Boolean
    9.  
    10.       ' If not already updating the checked listbox, continue...
    11.       If Not m_isUpdating Then
    12.          ' Flag us as updating the checked list to prevent recursion
    13.          m_isUpdating = True
    14.  
    15.          ' See if the item being checked/unchecked is the "All" item
    16.          If CheckedListBox1.SelectedItem.ToString() = "All" Then
    17.             ' If it is, check/uncheck all the other items
    18.             Dim index As Integer
    19.             ' Step through every item in the list
    20.             For index = 1 To CheckedListBox1.Items.Count - 1
    21.                ' Set the checked status of the item
    22.                CheckedListBox1.SetItemChecked(index, (e.NewValue = CheckState.Checked))
    23.             Next
    24.          ElseIf e.NewValue = CheckState.Unchecked Then
    25.             ' If this isn't the "All" item and we're unchecking something, then
    26.             ' Uncheck the "All" checkbox
    27.             CheckedListBox1.SetItemChecked(CheckedListBox1.Items.IndexOf("All"), False)
    28.          End If
    29.  
    30.          ' Reset the flag to indicate we're no longer updating the
    31.          ' checked listbox, so that it will execute the next time
    32.          ' the user checks/unchecks an item.
    33.          m_isUpdating = False
    34.       End If
    35.    End Sub

  10. #10

    Thread Starter
    Member
    Join Date
    Jun 2003
    Posts
    42

    Re: CheckedListBox Help

    Your the man....



    Once again THANK YOU very much...


    ScarEye


    Hopefully this will help another fellow n00b

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