ComboBox的SelectedIndexChanged事件:如何让pviously选择指数$ P $?指数、事件、ComboBox、SelectedIndexChanged

2023-09-07 04:21:33 作者:落花风吹续

我有一个用户来控制,一个组合框和一个SelectedIndexChanged事件处理程序。在事件处理程序中,我需要能够告诉什么是pviously选择指数$ P $ ......任何人都可以点我在正确的方向?

 私人无效cboTargetMode_SelectedIndexChanged(对象发件人,EventArgs的)
{
    //需要得到previously选择指数,并做一些处理在这里...
    // ...一些处理code在这里...


    开关(cboTargetMode.SelectedIndex)
    {
        案例1:// ......一些code在这里...
            打破;
        案例2:// ......一些code在这里...
            打破;
        案例3:// ......一些code在这里...
            打破;
        默认:// ......一些code在这里...
            打破;
    }
}
 

解决方案

没有什么内置的,您需要侦听此事件并跟踪类中的变量。

使用-1作为一个未初始化最后一个索引,等第一遍设置,但不使用它。后续的阶段,你使用它,并设置它。

使用派生组合框类你自己的,你总是可以做到这一点,并覆盖 OnSelectedIndexChanged 和暴露 previousSelectedIndex 属性。这样一来,就不会被紧密耦合的形式。或者,你可以做到这一点使用的事件,它也有资格实施作为扩展供应商。

c 中,如何对COMBOBOX的SelectedIndexChanged事件分别执行程序

I have a user control which has a ComboBox and a SelectedIndexChanged event handler. In the event handler, I need to be able to tell what was the previously selected index... can anyone point me in the right direction?

private void cboTargetMode_SelectedIndexChanged(object sender, EventArgs e)
{
    // need to get the previously selected index and do some handling here...
    // ... some handler code here ...


    switch (cboTargetMode.SelectedIndex)
    {
        case 1:  // ..... some code here...
            break;
        case 2:  // ..... some code here...
            break;
        case 3:  // ..... some code here...
            break;
        default: // ..... some code here...
            break;
    }
}

解决方案

There is nothing built in, you will need to listen for this event and keep track in a class variable.

Use -1 as an uninitialized "last index", so on first pass you set it but don't use it. Subsequent passes you use it and set it.

You could always do this using a derived ComboBox class of your own and override OnSelectedIndexChanged and expose a PreviousSelectedIndex property. This way, it wouldn't be tightly coupled to the form. Alternatively, as you can do this using events, its also eligible for implementation as an extender provider.