Solved

VB.NET 2005 Beta 1 Listbox selection problem

Posted on 2004-10-12
8
204 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now