即使不更改值也会触发SelectedValueChanged吗? [英] SelectedValueChanged fires even when the value is not changed?

查看:78
本文介绍了即使不更改值也会触发SelectedValueChanged吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个组合框,其中的事件SelectedValueChanged 与方法相关.我发现选择旧值(显示为其Text属性的值)时,它的工作原理是多么奇怪.它引发事件并调用方法!

我正在使用带有DropDownStyle = DropDownListAutoCompleteMode = SuggestAppendAutoCompleteSource = ListItems的组合框.如何防止这种情况发生?我不想那样!

它似乎与事件"SelectedValueChanged"的名称不一致,这意味着,如果所选值不变,则不会发生任何事情!非常感谢!

I have a combobox with the event SelectedValueChanged associated with a method. I have found how odd it works when I select the old value (the value displayed as its Text property). It raises the event and calls the method!

I''m using a combobox with DropDownStyle = DropDownList, AutoCompleteMode = SuggestAppend and AutoCompleteSource = ListItems. How do I prevent this from happening? I don''t want it that way!

It doesn''t seem to conform to the name of the event "SelectedValueChanged" that means if the selected value doesn''t change, nothing will happen! Thank you so much!

推荐答案

是的,这是ComboBox 的标准行为(在Winforms中).下拉控件后,即使您选择了先前选择的相同项目,也会触发SelectedValueChanged !我认为您必须保留currentSelectedValue ,然后将其与每次触发的温度进行比较.

(请注意,对于SelectedIndexChanged也是如此)


---------

只是想补充一点,这假设您使用的是数据绑定组合框.我尚未使用带有手动添加项的非数据绑定组合框检查过此行为.
Yeah, that is standard behavior with the ComboBox (in Winforms). Once you drop down the control, then even if you select the same item that was previously selected, SelectedValueChanged will fire! I reckon you''d have to keep a currentSelectedValue and then compare against that each time it fires.

(Note that it''s same for SelectedIndexChanged as well)


---------

Just wanted to add that this assumes you are using a databound combobox. I have not checked this behavior with a non-databound combobox with manually added items.


如果您阅读了MSDN页面 [ValueMember属性的值.因此,如果您不使用数据绑定,那么实际上可能并不是您需要的事件.

当然,这并不能解释您所遇到的问题,而是可以使用SelecetedIndexChanged事件.
If you read the MSDN page[^] for SelectedValue property you will see that the ''value'' it is referring to is the value of the ValueMember property. So it may not actually be the event that you need if you are not using data binding.

This doesn''t explain the problems you are seeing, of course but it mat be that you should be using the SelecetedIndexChanged event.


好的,Nishant解释了它是如何工作的.我只想添加一些实用笔记.

如果仅移动列表框的选择,则实际上不会触发选择.当您通过Enter或dbl单击确定确认选择时将触发该选项,因此该选项实际上显示在组合的编辑框部分中.无论新的还是旧的价值,触发此确认事件都是很合理的.

您可以轻松获得所需的效果:在事件处理程序中,记住旧值并将新值与旧值进行比较.如果它们相同-不执行任何操作,请立即退出处理程序.就这么简单.

—SA
OK, Nishant explained how it works. I just want to add couple of practical notes.

Actually selection is not fired if you simply move selection of the list box. It is fired when you confirm the selection by Enter or dbl-click, so it actually shown in the edit box part of the combo. It is quite reasonable to fire this event of confirmation, no matter is that new or old value.

You can easily get the effect you need: in the event handler, remember old value and compare new value with the old one. If they are the same — do nothing, exit the handler immediately. As simple as that.

—SA


这篇关于即使不更改值也会触发SelectedValueChanged吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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