开发者

ComboBox SelectedIndexChanged not always fired - how do I check when it is?

I've got two ComboBoxes (they're DevExpress ComboBoxEdits in actuality, but I think in this case the behaviour is the same) and when the first box's index changes, I want to fire the same event for the second box, so it will show the correct related data.

My problem is that half the time this works perfectly, but sometimes the second event is not fired. The code looks roughly like this:

ComboBox1_SelectedIndexChanged()
{
   ....
   ComboBox2.SelectedIndex = new_index;
}

So I'm not explicitly firing the ComboBox2_SelectedIndexChanged event, but relying on the SelectedIndex being updated. So why doesn't this fire every time? If I'm explicitly setting the index, wouldn't that fire the开发者_如何学Python event, if the new index and the old index are the same?

I found this question, only partially related but I imagine the root problem may be similar (the value isn't always changed as expected?) WinForms ComboBox SelectedIndexChanged not firing when typing few chars followed by Alt+Down

Thanks, need some elucidation on this!


I'm guessing the reason is because Combobox2's selected index already just happens to be set to new_index. I know you imply that that's not the case, but I don't see how it could be otherwise.

If you just want combo2 set to a new index so the indexChanged event will fire, this should get you there, assuming there are at least two items in the combo (and if there's only one, well, there's really no way to change that anyway)

ComboBox2.SelectedIndex = (ComboBox2.SelectedIndex + 1) % ComboBox2.Items.Count;

Or better yet, why don't you take the body of the indexChanged event handler, stick it into a method, then call it in place of manually setting ComBox2's selectedIndex.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜