I am trying to select an item from a list view and it should display each in a text box as I select it. I am able to select and get first item but when I select the second item I am getting an error,
“Argument out of range exception was unhandled, InvalidArgument=Value of ‘0’ is not valid for ‘index’.”
I have my below code, please help me..
public partial class Form2 : Form
{
List<Person> people = new List<Person>();
}
class Person
{
public string Name{ get; set; }
private void button2_Click(Object sender, EventArgs e)
{
Person p = new Person();
p.Name = textBox1.Text;
people.Add(p);
listBox1.Items.Add(p.Name);
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = people[listView1.SelectedItems[0].Index].Name;
}
}
1 Answer