473,406 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Add Remove Item sIn ComboBox !!!

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
8 11678
nukefusion
221 Expert 100+
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
Ramk
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
221 Expert 100+
@Ramk
Thanks Ramk, that was what I had meant to write!
Dec 29 '08 #4
@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
221 Expert 100+
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
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
221 Expert 100+
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
thank you amigo

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

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

Similar topics

5
by: Bart Nessux | last post by:
Hi, I am generating a list of file names... some of the files are locked by the OS (Windows XP) and I know the names of these files (NTUSER.DAT, ntuser.dat.LOG, etc.) But, I don't know their...
1
by: K. Byanjankar | last post by:
Hi, is there a way to remove item from cookie collection that has a key to it... -- Response.cookies("items")("item1") = "111111" Response.cookies("items")("item2") = "222222"...
2
by: MFRASER | last post by:
How do I go about looping through a hash table and removing items. I know how do this in a collectionbase, but can't iterate through the hash table with out getting an error. Here is my sample...
1
by: gce | last post by:
Hi, I have a tabstrip/tabcontrol and like to remove a tab when a certain choice is made. I found the TabStrip1.Items.Remove(TabStrip1.Items.Item(1)) command. But I would like to have it...
1
by: Craig Buchanan | last post by:
what is the fastest way to remove a value from a string array? something like: dim x as string() = {"A","B","C","D"} 'remove C x.Clear(x, x.IndexOf(x, "C"), 1) Questions:
1
by: Jeff User | last post by:
Hi ..net 1.1 I have a simple string array like string myArray = CallToWebServiceThat_Returns_Elements; I want to remove an item from the array and also remove its position. I can search the...
8
monirul arfin
by: monirul arfin | last post by:
Hi all, when we right click on a folder, a menu box is open , where is show " Open, Explore, Search, Send to, Cut, Copy etc" . And if we install a program , this program item will be shown in this...
2
by: Steve | last post by:
I am working on a program that works like a check in/check out system. There is a folder on a network drive that stores a bunch of vb programs. This program will check in and check out programs...
3
by: IgorM | last post by:
Hi I'm using the code below to allow user to browse through some xml document. System.IO.StreamReader sr = new System.IO.StreamReader(currentPath); System.Xml.XmlTextReader xr = new...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.