By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,991 Members | 1,887 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,991 IT Pros & Developers. It's quick & easy.

Updating a list box

P: 53
Hello All,

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

Expand|Select|Wrap|Line Numbers
  1.         private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
  2.         {
  3.  
  4.             int index = listBox1.SelectedIndex;
  5.             Console.WriteLine("Index is {0}",index );
  6.             listBox1.Items[index] = textBox1.Text; 
  7.         }
  8.  
No matter which item I select in the Listbox,the index ( listBox1.Items[index]) always it takes as -1. Error:::InvalidArgument = Value of '-1' is not a valid index.

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

Thank you all
Feb 10 '09 #1
Share this Question
Share on Google+
1 Reply


P: 12
use if(index>=0) and put your code inside it.
Feb 11 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.