Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,
I have two comboboxes like cbCountry and cbCity. I use BindingSource to fill them with a relational way. When cbCountry.SelectedIndex = -1 , cbCity's SelectedIndex property gets to -1 by using KeyDown event. There is no problem for this steps. But, although cbCountry and cbCity's SelectedIndex property = -1, user can select an item on cbCity without selecting any on cbCountry. So, I want to create a custom event to check cbCity's SelectedIndex property like that;

When cbCity's SelectedIndex property changed, the event look at if cbCountry.SelectedIndex = 1 or not. If cbCountry.SelectedIndex = -1, this event makes cbCity's SelectedIndex property -1 as well. Actually I have used a custom event for a combobox, but for this situation I need to use two comboboxes in one event I have to check cbCountry.SelectedIndex and to change cbCity's SelectedIndex property in this event.

So, how can I do that with an event like below that I use to control one Combobox control? At this example that controls only cbCity's KeyDown property and if the key equal to Delete, it makes cbCity's SelectedIndex property -1.

C#
internal void SetSingleComboboxIndex(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Delete)
           ((ComboBox)sender).SelectedIndex = -1;
    }


    internal void ClearSingleCombobox(ComboBox cb)
    {
           cb.KeyDown += new System.Windows.Forms.KeyEventHandler(SetSingleComboboxIndex);
    }
Posted
Updated 2-Aug-11 22:55pm
v2

you should be able to retrieve the ID of the comboBox in method SetSingleComboboxIndex.

If that ID = cbCountry and the selectedIndex = -1, set cbCity.Enabled to false; else to true;
 
Share this answer
 
The whole idea of "deselecting" seems quite weird to me; I did not really understand its purpose. However, I can see a different problems.

1) If the method ClearSingleCombobox is called repeatedly (as I would assume looking at the name of this method), you are in trouble. In this case every time you "clear", you're adding the same method to the event's invocation list. It can cause a really good memory leak at best.

2) Both methods you show don't have to be instance methods, they can be static as "this" parameter is unused because of cb. For performance reasons, if a method can be static it should be static. For example, Microsoft FxCop (recommended) will not pass this code.

—SA
 
Share this answer
 
Comments
H.Johnson 3-Aug-11 5:23am    
Dear SAKryukov,

thanks for reply. I just give my example to need such a kind of event. I do not called ClearSingleCombobox repeatedly and there is no problem related to this side. The only thing I want to do is; to prevent user select any item on cbCITY "if" cbCOUNTRY's SelectedIndex = -1. So, how can I do a custom event to control two comboboxes?

BR

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900