e.Item.SubItems.Text from a listview ItemCheckedEventArgs object
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.Text; // copies his value
The two lines copying into selectedPlayerInList work absolutely fine.
The copying into selectedPlayerValue doesn't work though.
e.Item.SubItems.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.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.
Re: e.Item.SubItems.Text from a listview ItemCheckedEventArgs object
From my understanding of listView is that the item counts as subitem and for any sub items it starts a subitem. I am sorry I am not able to explain this better but hopefully an example will clear things up.
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.
string playerName = lstPlayers.Items.Text;
string playerValue = lstPlayers.Items.SubItems.Text;
string oldValue = lstPlayers.Items.SubItems.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.