Updating a list box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sbandalli
    New Member
    • Feb 2009
    • 53

    Updating a list box

    Hello All,

    I am trying to udapte a listbox as suggested like this,but it gives me error.Please help...

    Code:
            private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                
                int index = listBox1.SelectedIndex;
                Console.WriteLine("Index is {0}",index );
                listBox1.Items[index] = textBox1.Text; 
            }
    No matter which item I select in the Listbox,the index ( listBox1.Items[index]) always it takes as -1. Error:::Invalid Argument = Value of '-1' is not a valid index.

    but when I print the index value using Console.writeLi ne it prints the correct slected index. I am not able to figure out the reason.

    Thank you all
    Last edited by pbmods; Feb 10 '09, 11:17 PM. Reason: Added CODE tags.
  • falaque
    New Member
    • Feb 2009
    • 12

    #2
    use if(index>=0) and put your code inside it.

    Comment

    Working...