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

Add Remove Item sIn ComboBox !!!

P: 24
dear friends
I want to add and remove items with this simple code :
Expand|Select|Wrap|Line Numbers
  1. private void comboBox1_KeyDown(object sender, KeyEventArgs e)
  2.         {
  3.  
  4.             ComboBox c = sender as ComboBox;
  5.             if (c == null) return;
  6.             if (c.Text == "") return;
  7.             if (e.KeyCode == Keys.Delete)
  8.             {
  9.                 c.Items.RemoveAt(c.FindStringExact(c.Text));
  10.             }
  11.             else if (e.KeyCode == Keys.Enter)
  12.                 c.Items.Add(c.Text);
  13.         }

this exception appears after add and remove some data and Leave the control on Application.Run(new Form1()); method;

InvalidArgument=Value of '0' is not valid for 'index'.


thank you.
Dec 28 '08 #1
Share this Question
Share on Google+
8 Replies


nukefusion
Expert 100+
P: 221
You'll need to check that the item exists in the combo-box before you try to delete it. At the moment you can type the name of a non-existent item, hit delete and the program will crash.

Expand|Select|Wrap|Line Numbers
  1. private void comboBox1_KeyDown(object sender, KeyEventArgs e)
  2. {
  3.  
  4.     ComboBox c = sender as ComboBox;
  5.     if (c == null) return;
  6.     if (c.Text == "") return;
  7.     if (e.KeyCode == Keys.Delete)
  8.     {
  9.         int index = c.FindStringExact(c.Text);
  10.         if (index > -1) c.Items.RemoveAt(c.FindStringExact(c.Text));
  11.     }
  12.     else if (e.KeyCode == Keys.Enter)
  13.         c.Items.Add(c.Text);
  14. }
  15.  
Dec 29 '08 #2

P: 61
int index = c.FindStringExact(c.Text);
if (index > -1) c.Items.RemoveAt(c.FindStringExact(c.Text));
I guess the above line should be
Expand|Select|Wrap|Line Numbers
  1. if (index > -1) c.Items.RemoveAt(index);
Dec 29 '08 #3

nukefusion
Expert 100+
P: 221
@Ramk
Thanks Ramk, that was what I had meant to write!
Dec 29 '08 #4

P: 24
@nukefusion
Dear nukefusion
I Guess the problem is so much stranger than we think .
check this code :

Expand|Select|Wrap|Line Numbers
  1. private void comboBox1_KeyDown(object sender, KeyEventArgs e)
  2.         {
  3.  
  4.             ComboBox c = sender as ComboBox;
  5.             if (c == null) return;
  6.             if (c.Text == "") return;
  7.             if (e.KeyCode == Keys.Delete)
  8.             {
  9.                 c.Items.Clear();
  10.             }
  11.             else if (e.KeyCode == Keys.Enter)
  12.                 c.Items.Add(c.Text);
  13.         }
  14.  
same exception on Leave Event
Dec 29 '08 #5

nukefusion
Expert 100+
P: 221
I'm unable to recreate the exact exception you are having. I've created a simple form with a combo-box and a button and copied in the event handler code you provided. I can add, delete items, tab out of the combo-box, but cannot reproduce the error.

Do you have any code executing in the Leave event? Exactly what steps are you taking to reproduce this exception?
Dec 29 '08 #6

P: 24
I just have one combobox in my page with one keydown event

Expand|Select|Wrap|Line Numbers
  1. private void comboBox1_KeyDown(object sender, KeyEventArgs e)
  2.         {
  3.  
  4.             ComboBox c = sender as ComboBox;
  5.             if (c == null) return;
  6.             if (c.Text == "") return;
  7.             if (e.KeyCode == Keys.Delete)
  8.             {
  9.                 c.Items.Clear();
  10.             }
  11.             else if (e.KeyCode == Keys.Enter)
  12.                 c.Items.Add(c.Text);
  13.         }
Add 3 items

1 -> enter key
2 -> enter key
3 -> enter key


PullDown The Combobox
and Press Delete and Click on the Form to leave the combobox .

The Exception appears now!
Dec 30 '08 #7

nukefusion
Expert 100+
P: 221
Thanks for the extra information. Using your steps I've been able to reproduce the error. It looks like if you delete an item while the drop-down is open and then try and leave the control, the selected index doesn't get a chance to update itself.

You'll then get this error with the index parameter being equal to the index of
whatever item you deleted.

Really, the best way to add new items to a combo-box would be to use a separate control, i.e. a textbox, with a button to add that value to the list. The combo-box is only really intended for item selection. However, if you really want to do things this way, to get around this quirk you could handle the DropDownClosed event of the combo-box and do a quick range check yourself. Here's some example code:

Expand|Select|Wrap|Line Numbers
  1. private void comboBox1_DropDownClosed(object sender, EventArgs e)
  2. {
  3.     ComboBox c = sender as ComboBox;
  4.     if (c.SelectedIndex >= c.Items.Count) c.SelectedIndex = c.Items.Count - 1;
  5. }
  6.  
Using this method, when the drop-down closes if there is a mismatch between the selected index and the number of items in the list (an item has been deleted), then the last item in the list is selected.

There are still other issues with this method that you'll probably want to clear up. For example, try typing some text, and pressing enter while the drop-down is open. The item will be added to the list twice.
Dec 30 '08 #8

P: 24
thank you amigo

belive it or not you saved my life ;)
Dec 30 '08 #9

Post your reply

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