I've been looking for a solution since this morning, and even after reading tons of other threads on this subject it doesn't work for me. Without further ados let's check this code sample:
// Create Dictionary, Keys = Ids, Values = Names
Dictionary<int, string> ff_names = new Dictionary<int, string>();
ff_names.Add(0, "Cloud");
ff_names.Add(1, "Barret");
ff_names.Add(2, "Tifa");
ff_names.Add(3, "Aerith");
ff_names.Add(4, "Red XIII");
// Populating ListView
foreach( KeyValuePair<int, string> dict in ff_names )
{
ListViewItem lvi = new ListViewItem(dict.Key.ToString());
lvi.SubItems.Add(dict.Value);
listView1.Items.Add(lvi);
}
// Test Item Selection
listView1.Focus();
listView1.Select();
listView1.Items[0].Focused = true;
listView1.Items[0].Selected = true;
string s = listView1.SelectedItems.Count.ToString();
label1.text = s; // sadly, it's equal to 0;
textBox1.Text = listView1.SelectedItems[0].SubItems[0].Text; // program will crash
Technically, I would like to selection an item of the ListView and display one of its element in a textbox. It works when I select an item manually, but when I try to select programmatically like shown above it doesn't want to select anything, the SelectedItems count is equal to zero...
Thank you for you help and hope someone can find a solution to what I'm missing!
1
. – user2509901