none
Visual Basic - Problem with Listview

    Question

  • My code with problem on debug is:

    ListView1.Items.Add("")
    ListView1.Items(11).SubItems.Add("") <-- (Problem)
    with Message:

    InvalidArgument=Value of '11' is not valid for 'index'.

    Please help :|

    Monday, January 13, 2014 5:31 AM

Answers

  • Hi,

    You can add as many as you can,

    But basically the index start's at 0,

    If you are referring the setfocus of a control you can use tabindex,

    the TabIndex is differ from ItemIndex


    Tuesday, January 14, 2014 5:46 AM
  • You must not have 12 Items in the ListView1. The Items are a 0 based index. Meaning item 1 is at (0), 2 is at (1), 3 is at (2), ect. Check that you are using the correct index.
    Monday, January 13, 2014 5:41 AM
  • That second line is trying to add a subitem to the item with index 11, i.e the twelfth item.  The error message is telling that there is fewer items in the ListView than 12.  Presumably you want to add a subitem to the item you just added.  That first Add method returns a reference to that item, so you just use that reference.  Assign that reference to a variable and use it as many times as you like.
    Monday, January 13, 2014 5:41 AM
  • Hi,

    Welcome to MSDN.

    Here are already a lot helpful replies.

    In addition to them, which item will you add these SubItems to?

    ListView1.Items.Add("")'<-(add to this item)?
    ListView1.Items(the index).SubItems.Add("") 

    If you want to add these SubItems to the one just added, you could code with this way:

        Dim itemTemp = ListView1.Items.Add("addedItem")
            itemTemp.SubItems.Add("add the SubItems to itemTemp")
    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, January 14, 2014 7:54 AM
    Moderator

All replies

  • You must not have 12 Items in the ListView1. The Items are a 0 based index. Meaning item 1 is at (0), 2 is at (1), 3 is at (2), ect. Check that you are using the correct index.
    Monday, January 13, 2014 5:41 AM
  • That second line is trying to add a subitem to the item with index 11, i.e the twelfth item.  The error message is telling that there is fewer items in the ListView than 12.  Presumably you want to add a subitem to the item you just added.  That first Add method returns a reference to that item, so you just use that reference.  Assign that reference to a variable and use it as many times as you like.
    Monday, January 13, 2014 5:41 AM
  • Because you don't have index 11, then probably what kind of view you referring for?

    http://msdn.microsoft.com/en-us/library/system.windows.forms.listview(v=vs.110).aspx

    Check out this sample:

    http://code.msdn.microsoft.com/List-View-Control-Visual-645117a6

    Or probably you meet this understanding:

    ListView1.Items.Add("sample")
    ListView1.Items(0).SubItems.Add("sub sample")


    Monday, January 13, 2014 5:41 AM
  • Can i add more items than 12?

    In properties i select ListView and in TabIndex I write 50.

    http://i.imgur.com/bvIkRe0.png

    Tuesday, January 14, 2014 5:40 AM
  • Hi,

    You can add as many as you can,

    But basically the index start's at 0,

    If you are referring the setfocus of a control you can use tabindex,

    the TabIndex is differ from ItemIndex


    Tuesday, January 14, 2014 5:46 AM
  • Hi,

    Welcome to MSDN.

    Here are already a lot helpful replies.

    In addition to them, which item will you add these SubItems to?

    ListView1.Items.Add("")'<-(add to this item)?
    ListView1.Items(the index).SubItems.Add("") 

    If you want to add these SubItems to the one just added, you could code with this way:

        Dim itemTemp = ListView1.Items.Add("addedItem")
            itemTemp.SubItems.Add("add the SubItems to itemTemp")
    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, January 14, 2014 7:54 AM
    Moderator