Solved

VB.NET 2005 Beta 1 Listbox selection problem

Posted on 2004-10-12
8
213 Views
Last Modified: 2010-04-23
Hi EveryOne!
I am using VB.NET 2005 beta 1. and trying to use listbox, and select them all. but i having problem with ListItem declaretion
here is the code


Private Sub chkSelectAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSelectAll.CheckedChanged

        Dim i As listitem   -------<<<<<< VB won't recongise ListItem even though, I looked this one in msdn vb example.


        If chkSelectAll.Checked Then

            For Each i In lstState.Items
                i.selected = True

            Next

        Else
            For Each i In lstState.Items
                i.selected = False

            Next

        End If

    End Sub
0
Comment
Question by:ExpertPro
  • 4
  • 2
  • 2
8 Comments
 
LVL 13

Assisted Solution

by:Corey2
Corey2 earned 100 total points
ID: 12291051
For this code to work I needs to be declared as an Object type

Dim I as Object

or

Dim I 'if you don't specify it is an object

FYI in vb.net  Variant type is now Object type

Corey
0
 
LVL 1

Author Comment

by:ExpertPro
ID: 12291604
Corey,
Thanks for your reply, I try to use as Obect still not working. Either its a problem becase of beta version or something else.
0
 
LVL 3

Accepted Solution

by:
jazduck earned 400 total points
ID: 12294563
The following works in 2005 beta1.  It does the same sort of thing, and is how I generally control popualtion of my listbox's.

I have one checkbox and one listbox on my form named as defaults, the rest should work by cut & paste.

Public Class Form1

    Structure ListItem
        Dim Index As Integer
        Dim Name As String
    End Structure
    Dim Item As New ListItem
    Dim AL As New ArrayList

    Private Sub chkSelectAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked Then
            For Each Item In AL
                ListBox1.SetSelected(Item.Index, True)
            Next
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Item.Index = 0
        Item.Name = "Index0"
        AL.Add(Item)
        Item.Index = 1
        Item.Name = "Index1"
        AL.Add(Item)
        Item.Index = 2
        Item.Name = "Index2"
        AL.Add(Item)
        Item.Index = 3
        Item.Name = "Index3"
        AL.Add(Item)
        Item.Index = 4
        Item.Name = "Index4"
        AL.Add(Item)
        For Each Item In AL
            ListBox1.Items.Add(Item.Name)
        Next
    End Sub
End Class
0
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 3

Expert Comment

by:jazduck
ID: 12294569
Oh, make sure your Listbox is set to MultiSimple or MultiExtended Selection mode, it sounds like a silly mistake, but i made it when i was writing this :)

Regards,
Jaz
0
 
LVL 3

Expert Comment

by:jazduck
ID: 12294627
You can also achieve the same thing in a lot less code, but it isn't OO :)

For i As Integer = 0 To (ListBox1.Items.Count - 1)
         ListBox1.SetSelected(i, True)
Next

Upto you as to which you prefer, while this is programatically simpler and incurs less performance overhead I personally prefer having an arraylist per listbox control, as it allows easy control & grouping of objects.  Should you wish to use a combobox tomorrow, its a simple matter of changing the loop that populates the control with the objects name.

Regards (again)
Jaz
0
 
LVL 3

Expert Comment

by:jazduck
ID: 12294952
Thought id add this lil routine I use as it saves the trouble of having to create the ArrayList manually in code (as i did in the first sample).

The routine takes any items already in the ListBox (so you can use the designer to populate your listbox), and reads the name and index's into an ArrayList.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ConvertListBoxToAL(ListBox1, AL1)
        'ConvertListBoxToAL(ListBox2, AL2)  'And so on if you have multiple controls
End Sub

Sub ConvertListBoxToAL(ByVal LBox As ListBox, ByVal AL As ArrayList)
        Dim ItemCount As Integer = LBox.Items.Count - 1
        For i As Integer = 0 To ItemCount
            LBox.SetSelected(i, True)
            Item.Index = i
            Item.Name = LBox.SelectedItem.ToString
            AL.Add(Item)
            LBox.SetSelected(i, False)
        Next
End Sub

Hope it Helps
Regards,
Jaz
0
 
LVL 13

Expert Comment

by:Corey2
ID: 12297373
Just wanted to point out the Obvious because even the most experienced programer sometimes over looks it.

Is it possible that i is already declared elsewhere and within the scope of this Sub.

Corey
0
 
LVL 1

Author Comment

by:ExpertPro
ID: 12339725
Thanks every one for your help.
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Opening and Closing Connections and Data Adapters 10 49
Help with AsEnumerable(), LINQ 4 39
Set value of dynamically created checkboxlist 2 33
VB.Net Report Printing Issue 3 33
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question