Why is the TextChanged event not firing when using up/down arrow keys to change ComboBox value in VB.Net Windows Forms?

huangapple go评论53阅读模式
英文:

Why is the TextChanged event not firing when using up/down arrow keys to change ComboBox value in VB.Net Windows Forms?

问题

我正在使用VB.Net Windows Forms应用程序。我在窗体上添加了一个ComboBox,将其"DropDownStyle"属性设置为DropDownList,并添加了一些项目。现在,当我运行应用程序并使用鼠标更改组合框的值时,一切都正常,TextChanged事件正常触发。但是,如果我使用上/下箭头键更改其值,TextChanged事件就不会触发。

有人能解释为什么吗?

问候,
Shishir Wahi

如果我将"DropDownStyle"属性更改为DropDown,则问题解决了,但我必须仅使用DropDownList样式。

英文:

I am working on a VB.Net Windows Forms application. I added a ComboBox to my form, set its "DropDownStyle" property to DropDownList and added a few items in it. Now, when I am running the application and changing the combo box value with the mouse, everything is working fine and TextChanged event is firing. But if I am changing its values using up/down arrow keys, the TextChanged event is not firing.

Can anybody explain why?

Regards,
Shishir Wahi

If I change the "DropDownStyle" property to DropDown then the issue is gone, but I have to use DropDownList style only.

答案1

得分: 0

用户如果将 DropDownStyle 设置为 DropDownList,则无法输入文本,因此 TextChanged 事件应该与之无关。只有在设置 Text 属性时才会触发该事件,但在这种情况下显然没有发生。当 DropDownStyle 设置为 DropDown 时,将触发该事件,因为控件基本上包含一个文本框,所以该事件的工作方式与 TextBox 控件相同。

根据具体情况,您应该处理 SelectIndexChangedSelectedValueChangedSelectionChangeCommittedTextUpdate 事件可能也可以满足您的需求,但可能不如其他事件合适。

英文:

The user can't enter text if DropDownStyle is set to DropDownList so the TextChanged event should be irrelevant. It will only be raised if the Text property gets set, which is clearly not happening in the case. It will be raised when DropDownStyle is set to DropDown because the control basically contains a text box, so the event works just as it would for a TextBox control.

You should be handling SelectIndexChanged, SelectedValueChanged or SelectionChangeCommitted, depending on the specific circumstances. The TextUpdate event might also give you what you want but is probably not as appropriate as the others.

huangapple
  • 本文由 发表于 2023年5月21日 18:26:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76299417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定