Results 1 to 2 of 2
  1. #1
    Silver Lounger
    Join Date
    Mar 2001
    Springfield, Ohio, USA
    Thanked 2 Times in 2 Posts

    CheckedListBox (VB.NET 2002)

    I'm trying to use a CheckedListBox, but cannot figure it out. If I try to use the CheckedItemCollection or the CheckedIndexCollection, it whines that it cannot be used as an expression! How do you iterate through the selected items? How do you use these collections? Also, how do you load strings into the list at run-time? I would prefer a few lines of VB code, but I can probably decipher C# code. TIA --Sam
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  2. #2
    5 Star Lounger jujuraf's Avatar
    Join Date
    Jun 2001
    San Jose, California, USA
    Thanked 0 Times in 0 Posts

    Re: CheckedListBox (VB.NET 2002)

    I'd highly recommend the only .NET GUI book I could find... "User Interfaces in VB.Net: Windows Forms and Custom Controls" by Matthew MacDonald. Page 95-98 (and others) have examples of list boxes (adding items, reading, etc.) This book has helped me maintain some sanity <img src=/S/nuts.gif border=0 alt=nuts width=15 height=15> as I too wade through all the non-trivial details of building a real application.

    I'm still struggling with the datagrid myself but that's another topic (I'd like a book on that one control actually).

    I haven't used CheckListBox just regular ListBox so I created a new project to try it out. To add hard-coded items in the list you select the Items property in the Property Window says "(Collection)..." This is a collection and a menu pops up for you to add whatever you want (one string per line). Ths isn't as lame as it seems because by adding these hardcoded items you can then look at the code generated and do it yourself for your own data. Expand the "#Regon" and look at the code for InitializeComponent(). It shows this:

    <pre>Me.chkList.Items.AddRange(New Object() {"Monday", "Tuesday", "Wednesday"})

    To add items programmatically, you can use DataSource or add each item one at a time like this:

    <pre>Private Sub InitList()
    Dim Cities() As String = {"Seattle", "New York", "Phoenx", "Montreal"}

    Me.chkList.DataSource = Cities
    End Sub</pre>

    This was called from the form's 'Public Sub New()' code after the InitializeComponent() call (automatically created for you when you create a Windows project).

    To figure out which item was checked do this:
    <pre>Public Sub WasChecked()
    Dim st As String

    For Each st In Me.chkList.CheckedItems
    ' do whatever here
    MessageBox.Show("You checked " & st)
    End Sub</pre>

    It's up to you to determine what event/action to use before readng the list of checked items (maybe some OK button?)
    HTH until you can get that book. <img src=/S/bravo.gif border=0 alt=bravo width=16 height=30>


Posting Permissions

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