【发布时间】:2011-11-13 13:41:37
【问题描述】:
我在表单上有一个 ComboBox。 ComboBox 的DropDownStyle 属性设置为DropDown,以便用户可以从下拉列表中选择一个项目或手动输入一些文本。
当用户从下拉列表中选择一个项目时,我想在项目的文本出现在组合框的文本字段之前对其进行一些更改。举一个非常简单的例子,假设下拉列表包含由 ID 和描述组成的项目,如下所示:
101 Cat
102 Dog
103 Bird
When one of these items is selected, I'd like only the description to appear in the ComboBox's text field. So when "102 Dog" is selected, the string "Dog" should be displayed in the text field, ready to be edited by the user, and the items in the drop down list should be unchanged.
我想我可以只听 ComboBox 的 SelectionChangeCommitted 事件,然后将 ComboBox 的 Text 属性设置为我喜欢的任何值。但如果这样做,我对Text 所做的更改将被忽略,整个字符串(“102 Dog”)仍会显示在 ComboBox 中。
然后我想我也应该将SelectedIndex 字段更新为-1,以向组合框表明我正在设置的Text 不是下拉列表中的项目。但这只会完全清除文本字段,无论我将 Text 属性更改为什么。
然后我认为SelectionChangedCommitted 是使用错误的事件,因为它似乎为我的目的太快触发(Text 属性似乎只更新我的选择之后 SelectionChangeCommitted 事件处理程序已完成)。但所有其他 ComboBox 事件也无法正常工作,包括 SelectedIndexChanged 和 DropDownClosed。
我认为这很容易实现。必须有一个简单的方法来做到这一点,我确定我错过了一些明显的东西......有什么想法吗?
【问题讨论】: