Checked list box selection

TomL

Member
Joined
Sep 22, 2009
Messages
13
Programming Experience
Beginner
More out of curiosity than necessity

How would you check all boxes above a selected box, and by that also check all boxes below a selected box.

While playing around i managed an invert selection method

VB.NET:
For i As Integer = 0 To Me.Checkedlistbox.Items.Count - 1
            If Me.Checkedlistbox.GetItemCheckState(i) = CheckState.Checked Then
                Me.Checkedlistbox.SetItemChecked(i, False)
            Else
                Me.Checkedlistbox.SetItemChecked(i, True)
            End If
        Next

and wondered if the above was possible



TomL
 
Select,

All but Selected one:
VB.NET:
       For i As Integer = 0 To CheckedListBox1.Items.Count - 1
            If CheckedListBox1.GetSelected(i) = False Then
                CheckedListBox1.SetItemChecked(i, True)
            End If
        Next

All but Checked one:
VB.NET:
       For i As Integer = 0 To CheckedListBox1.Items.Count - 1
            If CheckedListBox1.GetItemChecked(i) = False Then
                CheckedListBox1.SetItemChecked(i, True)
            End If
        Next
 
Thanks alot kulrom

I have these, what i dont have is check all boxes above selection and check all boxes below selection, using a right click toolstrip menu

see attatched picture
 

Attachments

  • Screen01.jpg
    Screen01.jpg
    18.6 KB · Views: 55
mark UP:
VB.NET:
      For i As Integer = 0 To CheckedListBox1.Items.Count - 1
            If i [COLOR=Green][B]>[/B][/COLOR] CheckedListBox1.SelectedIndex Then
                If CheckedListBox1.GetItemChecked(i) = False Then
                    CheckedListBox1.SetItemChecked(i, True)
                End If
            End If
        Next

mark DOWN:
VB.NET:
      For i As Integer = 0 To CheckedListBox1.Items.Count - 1
            If i [COLOR=Green][B]<[/B][/COLOR] CheckedListBox1.SelectedIndex Then
                If CheckedListBox1.GetItemChecked(i) = False Then
                    CheckedListBox1.SetItemChecked(i, True)
                End If
            End If
        Next
 
Ok thats fantastic

I was struggling with all sorts of methods, including index from point,

But this is so simple and thanks alot for it, appreciate the help
 
If you don't need to reset the other items you just loop from that index and up or down. There is also no need to get the checked state of the item before you set it. Neither of these optimizations would likely have any noticable impact in this case, but the code gets cleaner, and it's usually also a better coding practice to only process what you need to.
VB.NET:
For i As Integer = 0 to CheckedListBox1.SelectedIndex - 1
    CheckedListBox1.SetItemChecked(i, True)
Next

For i As Integer = CheckedListBox1.SelectedIndex + 1 To CheckedListBox1.Items.Count -1
    CheckedListBox1.SetItemChecked(i, True)
Next
 
Back
Top