|
-
Oct 30th, 2007, 11:58 AM
#1
Thread Starter
Member
[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
-
Oct 30th, 2007, 12:01 PM
#2
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
-
Oct 30th, 2007, 12:04 PM
#3
Thread Starter
Member
Re: simple question...what am I missing
When I do that it only displays one item not all that are sellected
-
Oct 30th, 2007, 12:22 PM
#4
Fanatic Member
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.
Using Visual Studio 2008
Please mark your thread RESOLVED if you no longer need help.
-
Oct 30th, 2007, 12:29 PM
#5
Thread Starter
Member
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
-
Oct 30th, 2007, 12:40 PM
#6
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
-
Oct 30th, 2007, 12:41 PM
#7
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
-
Oct 30th, 2007, 12:42 PM
#8
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?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|