?
Solved

VB.NET 2005 Beta 1 Listbox selection problem

Posted on 2004-10-12
8
Medium Priority
?
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 13

Assisted Solution

by:Corey2
Corey2 earned 400 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 1600 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month8 days, 17 hours left to enroll

764 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