Results 1 to 6 of 6

Thread: Listbox check if an item is selected

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2009
    Posts
    25

    Listbox check if an item is selected

    Iv been trying to figure this out for hours ok so basically i need a to check if there is an item selected in listbox1

    Heres the code in trying to make work
    Code:
    If ListBox1.SelectedItem = True Then
                ListBox2.Items.RemoveAt(ListBox1.SelectedIndex)
                ListBox3.Items.RemoveAt(ListBox1.SelectedIndex)
                ListBox4.Items.RemoveAt(ListBox1.SelectedIndex)
                ListBox5.Items.RemoveAt(ListBox1.SelectedIndex)
                ListBox6.Items.RemoveAt(ListBox1.SelectedIndex)
                ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
            ElseIf MsgBox("Please select a username") Then
            End If

  2. #2
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Listbox check if an item is selected

    Just use the selected index property:

    vb Code:
    1. If Me.ListBox1.SelectedIndex >= 0 then
    2. 'something is selected
    3. Else
    4. 'Nothing is
    5. End If

    Justin
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  3. #3
    Member
    Join Date
    Jun 2010
    Posts
    55

    Re: Listbox check if an item is selected

    You just need to check whether the selected index is not -1. So...

    vb Code:
    1. If Not myListbox.SelectedIndex = -1 Then
    2. 'Do something
    3. End If

  4. #4
    Member
    Join Date
    Jun 2010
    Posts
    55

    Re: Listbox check if an item is selected

    Justin your always to fast for me

  5. #5
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Listbox check if an item is selected

    Lol, yeah I hate it when that happens... But regardless, we both provided a suitable solution : ).

    Justin
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Listbox check if an item is selected

    on heels of that... there's a reason that this doesn't work:
    If ListBox1.SelectedItem = True Then

    SelectedItem returns a ListViewItem ... not a boolean...

    If ListBox1.SelectedItem IsNot Nothing Then
    ...


    -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??? *

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