Solved

VB.NET 2005 Beta 1 Listbox selection problem

Posted on 2004-10-12
8
216 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

791 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