Solved

VB.NET 2005 Beta 1 Listbox selection problem

Posted on 2004-10-12
8
210 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
System32Int Error 8 57
Round up to 100% in .NET 10 89
Help with converting data format in xml file using VB.NET 11 54
Red error squiggly on vb.net 7 27
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 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

19 Experts available now in Live!

Get 1:1 Help Now