如何使用自定义事件控制2个ComboBox? [英] How to control 2 ComboBoxes with a Custom Event?

查看:96
本文介绍了如何使用自定义事件控制2个ComboBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有两个组合框,例如cbCountry和cbCity.我使用BindingSource用关系方式填充它们.当cbCountry.SelectedIndex = -1时,cbCity的SelectedIndex属性通过使用KeyDown事件变为-1.此步骤没有问题.但是,尽管cbCountry和cbCity的SelectedIndex属性= -1,但用户可以在cbCity上选择一个项目,而无需在cbCountry上选择任何项目.因此,我想创建一个自定义事件来检查cbCity的SelectedIndex属性,例如;

当cbCity的SelectedIndex属性更改时,事件将查看cbCountry.SelectedIndex = 1与否.如果cbCountry.SelectedIndex = -1,则此事件也会使cbCity的SelectedIndex属性也为-1.实际上,我已经为组合框使用了自定义事件,但是对于这种情况,我需要在一个事件中使用两个组合框,我必须检查cbCountry.SelectedIndex并在此事件中更改cbCity的SelectedIndex属性.

因此,如何使用下面的事件来控制一个Combobox控件来做到这一点?在此示例中,仅控制cbCity的KeyDown属性,并且如果键等于Delete,它将使cbCity的SelectedIndex属性为-1.

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.

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);
    }

推荐答案

,您应该能够在方法SetSingleComboboxIndex中检索comboBox的ID.

如果该ID = cbCountry并且selectedIndex = -1,则将cbCity.Enabled设置为false;否则,将cbCity.Enabled设置为false.否则为真;
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;


整个取消选择"的想法对我来说似乎很奇怪;我真的不明白它的目的.但是,我看到了另一个问题.

1)如果方法ClearSingleCombobox被重复调用(就像我假设查看此方法的名称一样),那么您会遇到麻烦.在这种情况下,每次清除"时,都将相同的方法添加到事件的调用列表中.充其量只能导致非常好的内存泄漏.

2)您显示的两个方法不必一定是实例方法,它们可以是静态的,因为由于cb,未使用"this"参数.出于性能原因,如果方法可以是静态的,则它应该是静态的.例如,Microsoft FxCop(推荐)将不会传递此代码.

—SA
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


这篇关于如何使用自定义事件控制2个ComboBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆