Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multi select listbox 1

Status
Not open for further replies.

JonoB

Programmer
May 29, 2003
147
GB
Just started with Visual Basic Express 2008, after coming from an Access and VBA background.

I am trying to do is loop through a multi-select listbox and retrieve all the selected items, but it doesnt want to play nice.

What I have tried:

***********************
For Each varItem In Me.ListBox1.SelectedItems

MsgBox(Me.ListBox1.Items.Item(varItem))

Next varItem

***********************

And I get the error message: Conversion from type 'DataRowView' to type 'Integer' is not valid.

I have also tried:
***********************
If Me.ListBox1.SelectedItems.Count <> 0 Then
' If so, loop through all checked items and print results.
Dim x As Integer
Dim s As String = ""
For x = 0 To ListBox1.SelectedItems.Count - 1
s = s & "Checked Item " & CStr(x + 1) & " = " & _
CStr(Me.ListBox1.SelectedItems(x)) & ControlChars.CrLf
Next x
MessageBox.Show(s)
End If
***********************

The listbox is a single column bound item with string fields.

What am I missing?

 
Try changing:

Code:
CStr(Me.ListBox1.SelectedItems(x)) & ControlChars.CrLf

to

Code:
CType(Me.ListBox1.SelectedItems(x), DataRowView).Item("DataColumnName").ToString() & ControlChars.CrLf
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top