e.Item.SubItems[0].Text from a listview ItemCheckedEventArgs object
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: e.Item.SubItems[0].Text from a listview ItemCheckedEventArgs object

  1. #1
    Join Date
    Feb 2012
    Posts
    10

    e.Item.SubItems[0].Text from a listview ItemCheckedEventArgs object

    Hello

    I'm trying to copy info from one two-column listview control to another when the checkbox is selected.

    listViewPlayerList contains a list of footballers, their country flag, and a value (as well as a checkbox)

    I've set up a listViewPlayerList_ItemChecked(object sender, ItemCheckedEventArgs e) event to check for the box being checked...no problem there.



    I then (among various other things) attempt to copy the info stored in "e" into a new ListViewItem called selectedPlayerInList, and a new ListViewItem.ListViewSubItem called selectedPlayerValue

    I do this with these three lines of code

    selectedPlayerInList.ImageIndex = e.Item.ImageIndex; // copies the flag
    selectedPlayerInList.Text = e.Item.Text; // copies the player's name
    selectedPlayerValue.Text = e.Item.SubItems[0].Text; // copies his value


    The two lines copying into selectedPlayerInList work absolutely fine.

    The copying into selectedPlayerValue doesn't work though.

    e.Item.SubItems[0].Text returns the same text as e.Item.Text.


    the graphic below is an example...

    I've seleced Iker Casillas as my goalkeeper, and rather than copying across "4m", it's copied "Iker Casillas" again. And I have checked by displaying the value of e.Item.SubItems[0].Text, and that's what it contains.



    (ignore the other players that look to be working. I set those up as default values)


    I am something of a C# newbie, just studying from home, so I think I may have made some kind of schoolboy error, but I can't see where.

  2. #2
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    71

    Re: e.Item.SubItems[0].Text from a listview ItemCheckedEventArgs object

    From my understanding of listView is that the item counts as subitem[0] and for any sub items it starts a subitem[1]. I am sorry I am not able to explain this better but hopefully an example will clear things up.

    Example setup
    Code:
    	Player		Value		Old Value
    	0		1		2
    0	Iker C.		4M		2M
    1	Rui P.		3M		1M
    2	Ivan K.		2M		1M
    Now let say you want to access the Iker and want all his information.
    Code:
    string playerName = lstPlayers.Items[0].Text;
    string playerValue = lstPlayers.Items[0].SubItems[1].Text;
    string oldValue = lstPlayers.Items[0].SubItems[2].Text;
    
    // Display a message box with info to show example
    MessageBox.Show(playerName + " Val: " + playerValue + " Old val:" + oldValue);
    I apologize for not being able to describe it better but hope it helps you out.

  3. #3
    Join Date
    Feb 2012
    Posts
    10

    Re: e.Item.SubItems[0].Text from a listview ItemCheckedEventArgs object

    Thanks a lot.

    I tried using e.Item.Subitems[1].Text and it now works perfectly.


    Seems kinds of bizarre, and I was thrown by the 2010 Express Collection Editor which showed it using [0] when I had test data set up in the there...




    Strange that it shows [0] there, but you need to use [1] to access it.


    At least I know know, and that's the main thing.


    Thanks again, Richard.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center