PDA

View Full Version : VB list Box homework help


awaj
04-16-2009, 08:32 PM
Hopefully someone can help me out in like 4 hours.

The assignment is in VB 2005 and consists of several steps. the first is to get something selected in a list box to appear in a label when a button is clicked. I got that coded with out a problem. The next part of the assignment is where I have trouble. We are supposed to take multiple items from the list box and insert them into the label. I tried Me.Label.Text = Me.xListBox.SelectedItems.ToString and got a strange output. System.Windows.Forms.ListBox+SelectedObjectColecti onthanks for the help.

for those who are interested, the code looks like

Private Sub xMultiButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xMultiButton.Click
Me.xResultLabel.Text = Nothing

Me.xResultLabel.Text = Me.xNamesListBox.SelectedItems.ToString

End Sub

Paul Komski
04-16-2009, 10:37 PM
It sounds like you need a multi select listbox and then loop through the items just collecting the selected items into a string to display as a label's text.


Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim intN As Integer, strCarry As String
strCarry = ""
For intN = 0 To Me.ListBox1.Items.Count - 1
If Me.ListBox1.GetSelected(intN) Then
strCarry = Me.ListBox1.Items.Item(intN).ToString & " " & strCarry
End If
Next intN
Me.Label1.Text = strCarry
End Sub

awaj
04-16-2009, 11:26 PM
thanks, I'll give it a try and post back with results.

awaj
04-17-2009, 12:24 AM
Thank you for the help, I had to modify it so it worked with the click button. I wish I had thought of that for the code... Why did I get this message anyways?System.Windows.Forms.ListBox+SelectedObjec tColection

Paul Komski
04-17-2009, 03:32 AM
As you type-in me.xListBox.SelectedItems and then add a dot you should get a drop down list. When you scroll down to the bottom and select (by single click) the .ToString value its description should be briefly shown to you in a message baloon. The same is obviously true of any item. The items collection (that I used) and the selecteditems collection (that you used) of a list box are two distinctly different collections.

An image of these results for the object you referenced in your original text is shown below. That should give you your answer.

awaj
04-17-2009, 04:12 PM
ok, thank you for the explination of what happened