Results 1 to 14 of 14

Thread: (RESOLVED)when vb listboox1.selecteditem hits the last item listbox1.selectedindex=0

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    56

    (RESOLVED)when vb listboox1.selecteditem hits the last item listbox1.selectedindex=0

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    If ListBox1.SelectedIndex = (last item) Then
    Me.ListBox1.SelectedIndex = 0

    End If
    End Sub
    Last edited by TD5X; Mar 16th, 2011 at 02:49 AM.

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

    Re: when vb listboox1.selecteditem hits the last item listbox1.selectedindex=0

    Is that really a good idea? That means that you will never be able to select the last item.
    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
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: when vb listboox1.selecteditem hits the last item listbox1.selectedindex=0

    This is the time when you type words in order to make your post understandable, posting code (without code tags) and hoping that your title is sufficient, often is not.

    What you "appear" to be asking, is that when the user selects the last item in the listbox, that you actually select the first item. This would mean that your user can never select the last entry. Is this what you want? I can only guess what you are trying to do is create a roll-over method?

    The first place you should check is the documentation, which has a list of all the properties of a listbox control, of which one is items, which has a count property. As the Index is 0 based, you would have to -1 from the value.
    Code:
    ListBox1.Items.Count -1

  4. #4

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    56

    Re: when vb listboox1.selecteditem hits the last item listbox1.selectedindex=0

    Quote Originally Posted by Grimfort View Post
    This is the time when you type words in order to make your post understandable, posting code (without code tags) and hoping that your title is sufficient, often is not.

    What you "appear" to be asking, is that when the user selects the last item in the listbox, that you actually select the first item. This would mean that your user can never select the last entry. Is this what you want? I can only guess what you are trying to do is create a roll-over method?

    The first place you should check is the documentation, which has a list of all the properties of a listbox control, of which one is items, which has a count property. As the Index is 0 based, you would have to -1 from the value.
    Code:
    ListBox1.Items.Count -1
    i didnt have the right words to explain what i was trying to say and i always google and check msdn before i post i cant find most of the stuff im looking for there so i post here

    ty

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    56

    Re: when vb listboox1.selecteditem hits the last item listbox1.selectedindex=0

    Quote Originally Posted by jmcilhinney View Post
    Is that really a good idea? That means that you will never be able to select the last item.
    i just made the last line a bunch of Os

  6. #6
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: when vb listboox1.selecteditem hits the last item listbox1.selectedindex=0

    Thats fair enough, however I found the answer using your own words in your first post. You can use a question in google, for example 'vb.net last item in a listbox' and the 7th item down was this (how can i get to the last item in a listbox). I am not trying to tell you off, more explain where to look to help you get answers yourself first, apologies if it did not come across that way.
    Last edited by Grimfort; Mar 11th, 2011 at 05:01 AM. Reason: Dodgy spelling

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

    Re: when vb listboox1.selecteditem hits the last item listbox1.selectedindex=0

    Quote Originally Posted by TD5X View Post
    i just made the last line a bunch of Os
    Sounds like a hack to me.
    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

  8. #8
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: when vb listboox1.selecteditem hits the last item listbox1.selectedindex=0

    I have supplied the line to test if you are at the last item, so you should now be able to get your code working. As already highlighted, there may be another way to do what you are trying to achieve, all we need is for you to tell us if you want advice .

  9. #9

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    56

    Re: when vb listboox1.selecteditem hits the last item listbox1.selectedindex=0

    Quote Originally Posted by Grimfort View Post
    I have supplied the line to test if you are at the last item, so you should now be able to get your code working. As already highlighted, there may be another way to do what you are trying to achieve, all we need is for you to tell us if you want advice .
    had to change it up a little but everything works well :]

    Dim s1234 As String
    s1234 = ListBox1.Items.Count - 1

    thanks for the example

  10. #10

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    56

    Re: when vb listboox1.selecteditem hits the last item listbox1.selectedindex=0

    Quote Originally Posted by jmcilhinney View Post
    Sounds like a hack to me.
    depends what your definition of hacking is

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

    Re: when vb listboox1.selecteditem hits the last item listbox1.selectedindex=0

    Given that Count is an Integer, therefore (Count - 1) is an Integer, why are you declaring 's1234' as type String? Also, why use a silly name like 's1234' when you can use a meaningful name like 'itemCount'?
    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

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

    Re: when vb listboox1.selecteditem hits the last item listbox1.selectedindex=0

    Quote Originally Posted by TD5X View Post
    depends what your definition of hacking is
    The definition I'm currently employing is using a dodgy workaround to do something that you shouldn't really be doing in the first place.
    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

  13. #13

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    56

    Re: when vb listboox1.selecteditem hits the last item listbox1.selectedindex=0

    Quote Originally Posted by jmcilhinney View Post
    Given that Count is an Integer, therefore (Count - 1) is an Integer, why are you declaring 's1234' as type String? Also, why use a silly name like 's1234' when you can use a meaningful name like 'itemCount'?
    i was using s1234 as an example it works so im going to keep it that way and -1 doesnt work i had to use 0 like i was told by mr fox

  14. #14

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    56

    Re: when vb listboox1.selecteditem hits the last item listbox1.selectedindex=0

    Quote Originally Posted by jmcilhinney View Post
    The definition I'm currently employing is using a dodgy workaround to do something that you shouldn't really be doing in the first place.
    well then i am not hacking

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