T
tangokilo
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.SelectedItems(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.SelectedItems(1).ToString())
' Display the index of the first selected item in the ListBox.
System.Diagnostics.Debug.WriteLine(listBox1.SelectedIndices(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
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.SelectedItems(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.SelectedItems(1).ToString())
' Display the index of the first selected item in the ListBox.
System.Diagnostics.Debug.WriteLine(listBox1.SelectedIndices(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