ComboBox SelectedIndexChanged event: how to get the previously selected index?

前端 未结 6 1080
闹比i
闹比i 2021-01-17 11:35

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.

6条回答
  •  太阳男子
    2021-01-17 11:58

    There is nothing built in, you will need to listen for this event and keep track in an instance 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.

提交回复
热议问题