Listbox

hello,

How can I retrieve the values the user selected from a listbox with a selection mode set to = SelectionMode.MultiExtended?

[131 byte] By [Seppe001] at [2007-12-24]
# 1

Hi,

Use the follwoing code to select the items

Dim obj As Object

For Each obj In ListBox1.SelectedItems

MsgBox(obj.ToString)

Next

with regards,

Sadha Sivam S

Sadhasivam at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2
txs Sadhasivam,

But this doesn't give me the value, but a messagebox with "System.Data.DataRowView".

Seppe001 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3
What data are you trying to get at? From your post it looks like you've populated the ListBox with DataRowView instances. If there is a specific member or property in the DataRowView you need, just cast the object back to the DataRowView instance and access it directly.
JaredParsonsMSFT at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

From what you appear to have done - you need to specifically cast each item to a datarowview which is probably what was used in the databinding scenario.

The following code show demonstate this - most of the form load is creating a datatable and then a dataview from it which is being used for the databinding on the listbox.

The button click event will display the selected item values.

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable
dt.Columns.Add("col1")
dt.Columns.Add("col2")

dt.Rows.Add(1, 2)
dt.Rows.Add(3, 4)
dt.Rows.Add(5, 6)

'//Create a Listbox bound to a dataview type
Dim dv As DataView = dt.DefaultView
ListBox1.DataSource = dv
ListBox1.DisplayMember = "col1"
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim obj As DataRowView
For Each obj In ListBox1.SelectedItems
MsgBox(obj(0).ToString) '//Will display Value
Next
End Sub
End Class

spotty at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5
Thank zou spottz, the MsgBox(obj(0).ToString) did it!
Seppe001 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...