Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > VB Controls


Reply
 
Thread Tools Display Modes
  #1  
Old 05-15-2009, 08:40 AM
Code_Worm Code_Worm is offline
Newcomer
 
Join Date: May 2009
Posts: 2
Default VB Controls

Hey guys i'm new to the forum!

I was wondering if anyone had any resources on vb controls, I'm just a beginner so please excuse my little knowledge. I have been having troubles with the checked listbox control, selectedindex is the method for displaying the checked list position yeah? so what is the method that i need to use the actual vaue that is stored in the checked listbox ( the string ) ?

Please help!
Reply With Quote
  #2  
Old 05-15-2009, 09:36 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is online now
Ultimate Contributor

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 7,598
Default

The short version of my rant at the end of the post is that CheckedListBox is a complicated control and generally isn't worth it. You've already missed some details of how it works if you think the "Selected" properties and methods have anything to do with the check state, so let's discuss the control from a high level then look at some code examples.

Like a regular ListBox, CheckedListBox provides Items, SelectedIndex, SelectedItem, SelectedIndices, and SelectedItems properties. Here's what each one does:
  • Items gives you access to the value of all items, whether they are checked or selected.
  • SelectedIndex gives you the index of the currently selected item, whether it is checked or not.
  • SelectedIndices gives you a collection that contains the index of every selected item, whether it is checked or not.
  • SelectedItem gives you the currently selected item's value, whether it is checked or not.
  • SelectedItems gives you a collection that contains every selected item's value, whether it is checked or not.
  • GetSelected() tells you whether the item with the specified index is selected, whether it is checked or not.
  • SetSelected() lets you set whether an item with a specific index is selected or not.

The above are all you have to fool with if you're using a ListBox. In addition to these properties, here's the CheckedListBox-specific methods and properties:
  • CheckedIndices gives you a collection that contains the index to every checked item, whether it is selected or not.
  • CheckedItems gives you a collection that contains every checked item's value, whether it is selected or not.
  • GetItemChecked() returns a Boolean that indicates whether an item is checked.
  • GetItemCheckState() tells you whether an item is checked, unchecked, or invariant.
  • SetItemChecked() lets you check or uncheck an item.
  • SetItemCheckState() lets you make an item checked, unchecked, or invariant.

In a checked list box, an item can be in any one of 6 states based on whether it is selected, not selected, checked, unchecked, invariant, or any logical combination of selection and check state. You get items the same way you'd get items out of a ListBox: the Items property. You can use the FindString() or FindStringExact() methods to get the index of the item. FindString() looks for values that start with the string you pass, FindStringExact() looks for an exact match. Once you get the index, you have to use the various Get/Set functions to determine if the item is selected or checked.

Text is boring and examples speak louder, here's some code snippets that answer questions. All examples assume you're using strings as the values in the control:

"How do I get the value of the first selected item?"
Code:
If lstControl.SelectedIndices > 0 Then
    value = lstControl.SelectedIndices(0).ToString()
Else
    ' There are no selected values
End If
"How do I get the value of all checked items?"
Code:
value = lstControl.CheckedItems.ToArray() ' ToArray() is specific to VS 2008
"How do I get the value of the first item that is both checked and selected?"
Code:
' (Tougher than you might think, there's no guarantee there is such an item and
'  trying to use the CheckedIndices and SelectedIndices collection means you need to
'  maintain two indexes and perform a somewhat complicated loop.  Easier to use the brute
'  force method)
Dim value As String = Nothing

For i As Integer = 0 To lstControl.Items.Length - 1
    If lstControl.GetSelected(i) AndAlso lstControl.GetItemChecked(i) Then
        value = lstControl.Items(i)
        Exit For
    End If
Next

' If value is Nothing, there is no item that is both checked and selected.
Unrelated statement: I have no idea why CheckedListBox is so popular. It's hard to understand how to use it for both the programmer and the user. For most use cases, a multi-select ListBox behaves exactly the same way and is more familiar to the user. Quick quiz: how many CheckedListBox controls can you think of that are used in a Microsoft or other major software developer's program? I can think of 0. Lack of use usually indicates a reason, and in this case I'd say the reason is the control introduces complexity without adding much value.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 05-15-2009, 09:50 AM
wildfire1982 wildfire1982 is offline
Contributor
 
Join Date: Jul 2003
Location: Southampton UK
Posts: 795
Default

I agree entirely. In my most recent project I have added a date time picker with showCheckBox. I think this might go into the same sort of realms of confusion but im interested to see how users react to it.

Don't get me wrong, it serves a purpose but I dont think users will get to grips with it very quickly. I think you are totally 100% right but I cant help thinking... Is the reason that mainstream programs dont use the controls like this because they are confusing or is it confusing to users because main stream programs dont use them and they haven't had enough interaction with them?
__________________
Chris
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:

Powered by liquidweb