Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi All,

Im a NUBIE to VB.Net but am learning quickly. I have a listbox2 that is set to Selectionmode = Extended and need to capture the selected items in the list box to an array for a later action. I can easily get the first selected items text but as I iterate through the code it continues to populate the array with a repeat of the first text.

Any help or advice would be greatly appreciated.

VB
Dim RDFs = ListBox2.SelectedItems.Count

Dim RDFsArry(RDFs) As String
Dim index2 As Integer
index2 = 0
For Each selectedItems In ListBox2.SelectedIndex.ToString
    RDFsArry(index2) = (ListBox2.SelectedItem).ToString
    index2 = index2 + 1

Next
Posted
Updated 16-Sep-10 4:45am
v2
Comments
Kschuler 16-Sep-10 10:46am    
Edit - Fixed code block so it is only around code portion and set language of block to VB instead of SQL.

You are using the For Each incorrectly. It loops through a collection and ListBox2.SelectedIndex is not a collection. Try using ListBox2.SelectedIndices.
 
Share this answer
 
assuming that the items are string in the code below, but you need to go through the selectedindices of the listbox

VB
For Each item As String In ListBox2.SelectedIndices
  RDFsArray(index2) = item
  index2 = index2 + 1
Next
 
Share this answer
 
Comments
Dalek Dave 16-Sep-10 11:16am    
Good Answer
Simon_Whale 16-Sep-10 11:17am    
Thank you Mr Dalek Dave
Did not work, getting the following error now...

SelectedIndices' is not a member of System.Windows.Controls.ListBox
 
Share this answer
 
Comments
William Winner 16-Sep-10 11:15am    
That's because the standard ListBox that everyone uses comes from System.Windows.Forms.

The ListBox in System.Windows.Controls has a SelectedItems property that should do the same thing.

Oh...and you shouldn't post an answer that isn't answer...you can comment on someone else's answer just as I have done here.
BMWBear 16-Sep-10 11:47am    
William, Imports System.Windows.Controls is declared but I'm still getting the error.
William Winner 16-Sep-10 12:22pm    
That's why you're getting the error. If you are using the System.Windows.Controls Namespace, then you have to use SelectedItems, not SelectedIndices.

Though I don't know why you would declare System.Windows.Controls instead of System.Windows.Forms.
Use the SelectedItems property instead of SelectedIndex, as follows:

For Each item In ListBox2.SelectedItems
            RDFsArry(index2) = item
Next
 
Share this answer
 
v2
Comments
BMWBear 16-Sep-10 11:54am    
Marc, you codes does work but it is entering the following in to the array...
0 "S"
1 "Y"
2 "S"
3 "T"
4 "E"
BMWBear 16-Sep-10 12:03pm    
I figured it out, I dropped the .ToString at the end of SelectedItems and I'm now getting the values. Thanks everyone
Marc A. Brown 16-Sep-10 12:03pm    
Yeah, sorry about that typo. Caught at the same time you did. Fixed the answer.
C++
I figured it out, I dropped the .ToString at the end of SelectedItems and I'm now getting the values. Thanks everyone
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900