# C Sharp - SelectedIndex Event!!!



## McAvelli (Apr 19, 2008)

I want to put dhe datas to textboxes from listview items when I select a row.
It's all ok after the first selection occurs. But when I select again this error occurs: "*System.ArgumentOutOfRangeException*". Is there a way to reset the selecteditem to starting condition, after I select a row. Or maybe another solution, here's the code:

```
private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)

        {
            this.txtID.Clear();
            this.txtSurname.Clear();
            this.txtName.Clear();


	    this.txtID.Text = Convert.ToString(this.listView1.SelectedItems[0].SubItems[0].Text);
            this.txtSurname.Text = Convert.ToString(this.listView1.SelectedItems[0].SubItems[1].Text);
            this.txtName.Text = Convert.ToString(this.listView1.SelectedItems[0].SubItems[2].Text);

	}
```


----------



## JimmySeal (Sep 25, 2007)

Firstly, there is no need to clear the text boxes right before you put text into them, and there is no need to convert the Text property of the SubItems to a string, as it is already a string.

You can prevent the error by surrounding the code in an if block

```
if(listView1.SelectedItems.Count > 0 && listView1.SelectedItems[0].SubItems.Count >= 3)
{
   //code here
}
```
there may be a better solution, but that may be good enough, depending on how your program works.


----------

