By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,852 Members | 955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,852 IT Pros & Developers. It's quick & easy.

MultiExtended & databound Listbox problem

P: n/a
Hello and thanks for your help,

I have the following Listbox created in VisualStudio 2003 designer, desiring
to select multiple entries from that list:
-------------------------------
ListBoxUser.Location = New System.Drawing.Point(16, 240)
ListBoxUser.Name = "ListBoxUser"
ListBoxUser.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended
ListBoxUser.Size = New System.Drawing.Size(136, 147)
ListBoxUser.TabIndex = 6
-------------------------------

Programatically I bind data with the following code:
-------------------------------
ListBoxUser.DataSource = objDS.Tables("tblUsers")
ListBoxUser.DisplayMember = "UserName"
ListBoxUser.ValueMember = "UserId"
-------------------------------

Later I loop through the selected items from ListBoxUser and verify these
selected items do not exist on DB. When I execute the code below
-------------------------------
For uIdx = 0 To ListBoxUser.SelectedItems.Count - 1

System.Diagnostics.Debug.WriteLine(ListBoxUser.Sel ectedItems(uIdx).ToString(
))
strUser = ListBoxUser.SelectedItems(uIdx).ToString()
Next For
-------------------------------
Debug.WriteLine shows value of 'System.Data.DataRowView' instead of 'Fred'
or 'Charlie'; and so does the contents of strUser (declared as string).

However, when creating the same kind of listbox programmatically without
databinding and the display works fine, eg. `Item 4` and index value of `1`
are displayed/printed:
-------------------------------
Dim listBox1 As New ListBox
' Set the size and location of the ListBox.
listBox1.Size = New System.Drawing.Size(200, 100)
listBox1.Location = New System.Drawing.Point(10, 10)
' Add the ListBox to the form.
Controls.Add(listBox1)
' Set the ListBox to display items in multiple columns.
listBox1.MultiColumn = False
' Set the selection mode to multiple and extended.
listBox1.SelectionMode = SelectionMode.MultiExtended
' Shutdown the painting of the ListBox as items are added.
listBox1.BeginUpdate()
' Loop through and add 50 items to the ListBox.
Dim x As Integer
For x = 1 To 50
listBox1.Items.Add("Item " & x.ToString())
Next x
' Allow the ListBox to repaint and display the new items.
listBox1.EndUpdate()
' Select three items from the ListBox.
listBox1.SetSelected(1, True)
listBox1.SetSelected(3, True)
listBox1.SetSelected(5, True)
' Display the second selected item in the ListBox to the console.
System.Diagnostics.Debug.WriteLine(listBox1.Select edItems(1).ToString())
' Display the index of the first selected item in the ListBox.
System.Diagnostics.Debug.WriteLine(listBox1.Select edIndices(0).ToString())
-------------------------------

So what am I doing wrong? How can I get the text/string values of the
selected items?

Many thanks for your help

Thomas
Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi,

For x As Integer = 0 To ListBox1.Items.Count - 1
Dim dr As DataRowView = DirectCast(ListBox1.Items(x),
DataRowView)
Debug.WriteLine(dr("UserName").ToString)
Next

Ken
------------------------
"tangokilo" <tk******@ozemail.com.au> wrote in message
news:bR**************@nnrp1.ozemail.com.au...
Hello and thanks for your help,

I have the following Listbox created in VisualStudio 2003 designer,
desiring
to select multiple entries from that list:
-------------------------------
ListBoxUser.Location = New System.Drawing.Point(16, 240)
ListBoxUser.Name = "ListBoxUser"
ListBoxUser.SelectionMode =
System.Windows.Forms.SelectionMode.MultiExtended
ListBoxUser.Size = New System.Drawing.Size(136, 147)
ListBoxUser.TabIndex = 6
-------------------------------

Programatically I bind data with the following code:
-------------------------------
ListBoxUser.DataSource = objDS.Tables("tblUsers")
ListBoxUser.DisplayMember = "UserName"
ListBoxUser.ValueMember = "UserId"
-------------------------------

Later I loop through the selected items from ListBoxUser and verify these
selected items do not exist on DB. When I execute the code below
-------------------------------
For uIdx = 0 To ListBoxUser.SelectedItems.Count - 1

System.Diagnostics.Debug.WriteLine(ListBoxUser.Sel ectedItems(uIdx).ToString(
))
strUser = ListBoxUser.SelectedItems(uIdx).ToString()
Next For
-------------------------------
Debug.WriteLine shows value of 'System.Data.DataRowView' instead of 'Fred'
or 'Charlie'; and so does the contents of strUser (declared as string).

However, when creating the same kind of listbox programmatically without
databinding and the display works fine, eg. `Item 4` and index value of
`1`
are displayed/printed:
-------------------------------
Dim listBox1 As New ListBox
' Set the size and location of the ListBox.
listBox1.Size = New System.Drawing.Size(200, 100)
listBox1.Location = New System.Drawing.Point(10, 10)
' Add the ListBox to the form.
Controls.Add(listBox1)
' Set the ListBox to display items in multiple columns.
listBox1.MultiColumn = False
' Set the selection mode to multiple and extended.
listBox1.SelectionMode = SelectionMode.MultiExtended
' Shutdown the painting of the ListBox as items are added.
listBox1.BeginUpdate()
' Loop through and add 50 items to the ListBox.
Dim x As Integer
For x = 1 To 50
listBox1.Items.Add("Item " & x.ToString())
Next x
' Allow the ListBox to repaint and display the new items.
listBox1.EndUpdate()
' Select three items from the ListBox.
listBox1.SetSelected(1, True)
listBox1.SetSelected(3, True)
listBox1.SetSelected(5, True)
' Display the second selected item in the ListBox to the console.
System.Diagnostics.Debug.WriteLine(listBox1.Select edItems(1).ToString())
' Display the index of the first selected item in the ListBox.
System.Diagnostics.Debug.WriteLine(listBox1.Select edIndices(0).ToString())
-------------------------------

So what am I doing wrong? How can I get the text/string values of the
selected items?

Many thanks for your help

Thomas

Nov 20 '05 #2

P: n/a
Hi Thomas,

Probably the cases of the datanames between quotes, they are case sensetive.

If not, ask again?

Cor
Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.