|
-
Feb 4th, 2005, 05:08 PM
#1
Thread Starter
Member
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
-
Feb 4th, 2005, 07:20 PM
#2
Re: CheckedListBox Help
VB Code:
Private m_blnInLoop As Boolean
Private Sub ListView1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles ListView1.ItemCheck
Dim li As ListViewItem
If Not m_blnInLoop Then
m_blnInLoop = True
If ListView1.Items(e.Index).Text = "All" And e.NewValue = CheckState.Checked Then
Dim liSelect As ListViewItem
For Each liSelect In ListView1.Items
liSelect.Checked = True
Next
End If
m_blnInLoop = False
End If
End Sub
-
Feb 4th, 2005, 08:37 PM
#3
Re: CheckedListBox Help
Try:
VB Code:
Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
If CheckedListBox1.SelectedItem.ToString() = "All" And e.NewValue = CheckState.Checked Then
Dim index As Integer
For Each index In CheckedListBox1.CheckedIndices
CheckedListBox1.SetItemChecked(index, False)
Next
ElseIf e.NewValue = CheckState.Checked Then
CheckedListBox1.SetItemChecked(CheckedListBox1.Items.IndexOf("All"), False)
End If
End Sub
-
Feb 7th, 2005, 03:19 PM
#4
Thread Starter
Member
Re: CheckedListBox Help
 Originally Posted by Aaron Young
Try:
VB Code:
Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
If CheckedListBox1.SelectedItem.ToString() = "All" And e.NewValue = CheckState.Checked Then
Dim index As Integer
For Each index In CheckedListBox1.CheckedIndices
CheckedListBox1.SetItemChecked(index, False)
Next
ElseIf e.NewValue = CheckState.Checked Then
CheckedListBox1.SetItemChecked(CheckedListBox1.Items.IndexOf("All"), False)
End If
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
-
Feb 7th, 2005, 03:21 PM
#5
Thread Starter
Member
Re: CheckedListBox Help
 Originally Posted by Negative0
VB Code:
Private m_blnInLoop As Boolean
Private Sub ListView1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles ListView1.ItemCheck
Dim li As ListViewItem
If Not m_blnInLoop Then
m_blnInLoop = True
If ListView1.Items(e.Index).Text = "All" And e.NewValue = CheckState.Checked Then
Dim liSelect As ListViewItem
For Each liSelect In ListView1.Items
liSelect.Checked = True
Next
End If
m_blnInLoop = False
End If
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
-
Feb 7th, 2005, 05:52 PM
#6
Re: CheckedListBox Help
Sorry about that, guess I should have read more closely...
VB Code:
Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
Static m_isUpdating As Boolean
If Not m_isUpdating Then
m_isUpdating = True
If CheckedListBox1.SelectedItem.ToString() = "All" Then
Dim index As Integer
For index = 1 To CheckedListBox1.Items.Count - 1
CheckedListBox1.SetItemChecked(index, (e.NewValue = CheckState.Checked))
Next
ElseIf e.NewValue = CheckState.Unchecked Then
CheckedListBox1.SetItemChecked(CheckedListBox1.Items.IndexOf("All"), False)
End If
m_isUpdating = False
End If
End Sub
-
Feb 7th, 2005, 06:01 PM
#7
Thread Starter
Member
Re: CheckedListBox Help
Aaron,
You are the fkn MAN !
Thank you very much.
ScarEye
-
Feb 7th, 2005, 06:27 PM
#8
Thread Starter
Member
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
-
Feb 7th, 2005, 06:52 PM
#9
Re: CheckedListBox Help
Sure:
VB Code:
Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
' Create a static variable which retains it's value between
' calls to this event. We'll use this to know when we're
' in the process of checking/unchecking boxes so as not to
' cause an infinite loop (as this event is called when "any"
' item is checked/unchecked, even through code), preventing the
' dreaded Stack Overflow message.
Static m_isUpdating As Boolean
' If not already updating the checked listbox, continue...
If Not m_isUpdating Then
' Flag us as updating the checked list to prevent recursion
m_isUpdating = True
' See if the item being checked/unchecked is the "All" item
If CheckedListBox1.SelectedItem.ToString() = "All" Then
' If it is, check/uncheck all the other items
Dim index As Integer
' Step through every item in the list
For index = 1 To CheckedListBox1.Items.Count - 1
' Set the checked status of the item
CheckedListBox1.SetItemChecked(index, (e.NewValue = CheckState.Checked))
Next
ElseIf e.NewValue = CheckState.Unchecked Then
' If this isn't the "All" item and we're unchecking something, then
' Uncheck the "All" checkbox
CheckedListBox1.SetItemChecked(CheckedListBox1.Items.IndexOf("All"), False)
End If
' Reset the flag to indicate we're no longer updating the
' checked listbox, so that it will execute the next time
' the user checks/unchecks an item.
m_isUpdating = False
End If
End Sub
-
Feb 7th, 2005, 07:00 PM
#10
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|