Results 1 to 8 of 8

Thread: [RESOLVED] simple question...what am I missing

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2007
    Posts
    48

    Resolved [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

  2. #2
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    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

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2007
    Posts
    48

    Re: simple question...what am I missing

    When I do that it only displays one item not all that are sellected

  4. #4
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    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.

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2007
    Posts
    48

    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

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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:
    1. Private Sub multiButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    2.     Handles multiButton.Click
    3.  
    4.         Me.resultLabel.Text = String.Empty
    5.  
    6.         If Me.namesListBox.SelectedItems.Count > 0 Then
    7.             For Each itm As String In Me.namesListBox.SelectedItems
    8.                 Me.resultLabel.Text &= itm & Environment.NewLine
    9.             Next
    10.         End If
    11.  
    12. End Sub
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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
  •  



Click Here to Expand Forum to Full Width