[RESOLVED] simple question...what am I missing
I need to be able to select and display multiple items from a list box. I can get it to display one item, but I cant get multiple items to display. I thougt it would be just changing item to items but that's not the case. It then shows "System.Windows.Forms.ListBox.+SelectedObjectCollection" in my label.
Any help is greatly appericated!
Code:
Option Explicit On
Option Strict On
Public Class MainForm
Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' fills the list box with values
namesListBox.Items.Add("Debbie")
namesListBox.Items.Add("Bill")
namesListBox.Items.Add("Jim")
namesListBox.Items.Add("Ahmad")
namesListBox.Items.Add("Carol")
End Sub
Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click
Me.Close()
End Sub
Private Sub singleButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles singleButton.Click
resultLabel.Text = Convert.ToString(namesListBox.SelectedItem)
End Sub
Private Sub multiButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles multiButton.Click
resultLabel.Text = Convert.ToString(namesListBox.SelectedItems)
End Sub
End Class
Re: simple question...what am I missing
SelectedItem"s" is the entire collection.
You want SelectedItem
Edit: I see what you mean now.
Code:
For Each str As String In ListBox.SelectedItems
resultLabel.Text = String.Concat(resultLabel.Text, str, System.Environment.NewLine)
Next
Re: simple question...what am I missing
When I do that it only displays one item not all that are sellected
Re: simple question...what am I missing
I think you're getting an error because you're trying to assign a label with multiple list box items.
Re: simple question...what am I missing
I am on chapter 5 of the book that I am using to teach myself VB. The basics of this program I pulled of of the cd. I am only to add in the click events to display one selection then multiple selections.
Here is the quetion I am stuck on..directly from the book
Code the multiButton's click event procedure so that the it clears the contents of the resultLabel it should then display the selected names (wich are stored in the SelectedItems property) on seprate lines in the resultLabel
Re: simple question...what am I missing
The selectedItems property containing a collection of items that are selected.
Try hitting the . after SelectedItems... there's a host of properties... like .Count... .Item() ....
-tg
Re: simple question...what am I missing
i presume you have changed the Listbox so that its selectionMode property is set to multiple (either Multisimple or MultiExtended)
vb Code:
Private Sub multiButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles multiButton.Click
Me.resultLabel.Text = String.Empty
If Me.namesListBox.SelectedItems.Count > 0 Then
For Each itm As String In Me.namesListBox.SelectedItems
Me.resultLabel.Text &= itm & Environment.NewLine
Next
End If
End Sub
Re: simple question...what am I missing
SelectedItems is a collection of Listbox items, not strings. If you loop with a for each oItem as listboxitem then you can concatenate the .text property of each as it just may be easier to deduce that way for you.
Is this a school assignment?