Connecting Tech Pros Worldwide Help | Site Map

Add Remove Item sIn ComboBox !!!

Newbie
 
Join Date: May 2008
Posts: 24
#1: Dec 28 '08
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.
nukefusion's Avatar
Expert
 
Join Date: Mar 2008
Location: Essex, UK
Posts: 197
#2: Dec 29 '08

re: Add Remove Item sIn ComboBox !!!


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.  
Member
 
Join Date: Nov 2008
Posts: 58
#3: Dec 29 '08

re: Add Remove Item sIn ComboBox !!!


Quote:
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);
nukefusion's Avatar
Expert
 
Join Date: Mar 2008
Location: Essex, UK
Posts: 197
#4: Dec 29 '08

re: Add Remove Item sIn ComboBox !!!


Quote:

Originally Posted by Ramk View Post

I guess the above line should be

Expand|Select|Wrap|Line Numbers
  1. if (index > -1) c.Items.RemoveAt(index);

Thanks Ramk, that was what I had meant to write!
Newbie
 
Join Date: May 2008
Posts: 24
#5: Dec 29 '08

re: Add Remove Item sIn ComboBox !!!


Quote:

Originally Posted by nukefusion View Post

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.

if (index > -1) c.Items.RemoveAt(c.FindStringExact(c.Text));

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
nukefusion's Avatar
Expert
 
Join Date: Mar 2008
Location: Essex, UK
Posts: 197
#6: Dec 29 '08

re: Add Remove Item sIn ComboBox !!!


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?
Newbie
 
Join Date: May 2008
Posts: 24
#7: Dec 30 '08

re: Add Remove Item sIn ComboBox !!!


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!
nukefusion's Avatar
Expert
 
Join Date: Mar 2008
Location: Essex, UK
Posts: 197
#8: Dec 30 '08

re: Add Remove Item sIn ComboBox !!!


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.
Newbie
 
Join Date: May 2008
Posts: 24
#9: Dec 30 '08

re: Add Remove Item sIn ComboBox !!!


thank you amigo

belive it or not you saved my life ;)
Reply

Tags
add remove, combobox, item