Results 1 to 4 of 4

Thread: arguement out of range

  1. #1

    Thread Starter
    Hyperactive Member Grunt's Avatar
    Join Date
    Oct 2004
    Location
    Las Vegas
    Posts
    499

    arguement out of range

    why doesnt this work?

    Code:
    	Sub LstConnectionDataSelectedIndexChanged(sender As Object, e As EventArgs)
    		me.txtserver.text = Me.lstConnectionData.SelectedItems(0).text
    	End Sub
    i am getting an arguement out of range. using sharpdevelop and .net 3.5... I have multiselect set to false.

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

    Re: arguement out of range

    If the index 0 is out of range then there is no item at index 0, i.e. there are no items in the collection.

    Let me guess: lstConnectionData is a ListView, right? When you change the selection in a ListView the SelectedIndexChanged event is raised twice: once as the old item is deselected and once as the new item is selected. The first time there is no item selected so you have to ignore it, which would require an If statement that tests that there is at least one item selected.
    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
    Hyperactive Member Grunt's Avatar
    Join Date
    Oct 2004
    Location
    Las Vegas
    Posts
    499

    Re: arguement out of range

    there are items in the collection and it would be the second part of your post as it does work the first time. Just not if I select another item. I want to populate a textbox each time a new listviewitem is selected. How can I do that then?

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

    Re: arguement out of range

    You seem to have simply ignored what I said and stuck to what you assume is the case. First up, there can't possibly be items in the collection if index 0 is out of range. We're talking about the SelectedItems collection here, not the Items collection. Secondly, I've already told you what to do. If you just do it it will work. Add an If statement that tests the number of items in the collection, i.e. its Count property, and only try to access the first item IF there are items present.
    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

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