Results 1 to 6 of 6

Thread: [RESOLVED] List Box and Mouse Click Event While missing Selected item

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    17

    Resolved [RESOLVED] List Box and Mouse Click Event While missing Selected item

    Ok, I have a listbox that has a double click event on it. If you strike the items in the list the program works perfectly without a problem, but if you hit white space, the program blows up.
    The frmOpen has a rich text box that uses the data from transfername on it to call forth a name from another system.

    The question I have, Is there a way to show the system that there is nothing being clicked? I have tried the selecteditem = 0 and -1 and the selectedindex = 0 and -1 nothing happened. I'm kind of lost on this one. Trying to make this work for my new job.
    Code:
     Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.MouseDoubleClick
            Dim transfername As String = ListBox1.SelectedItem.ToString()
            frmOpen.Show()
            frmOpen.filename = transfername
        End Sub
    VB.net 2010 Professional Edition.
    Last edited by davebold370; Jun 15th, 2013 at 11:28 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: List Box and Mouse Click Event While missing Selected item

    If no item is selected then what's the value of SelectedItem? Have you actually checked that? What is usually used in VB to represent no object?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    17

    Re: List Box and Mouse Click Event While missing Selected item

    It's referenced as a null object. How do I make a test for that null object with a list box? I'm not to sure how to do that.

    Never Mind, I found a try catch. Thanks for the help.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: List Box and Mouse Click Event While missing Selected item

    Quote Originally Posted by davebold370 View Post
    It's referenced as a null object. How do I make a test for that null object with a list box? I'm not to sure how to do that.

    Never Mind, I found a try catch. Thanks for the help.
    NOOOOOOOOOO!!! That is a terrible solution. No object in VB is Nothing. All you have to do is test whether the SelectedItem Is Nothing.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    17

    Re: [RESOLVED] List Box and Mouse Click Event While missing Selected item

    So, Selecteditem = ""

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] List Box and Mouse Click Event While missing Selected item

    I'm looking back at my posts and I'm struggling to find where I said that you should compare the SelectedItem to an empty String. I said Nothing and I meant Nothing. You need to do exactly what I said to do:
    All you have to do is test whether the SelectedItem Is Nothing.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

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