简体   繁体   English

Combobox Text&SelectedIndex一致性/行为

[英]Combobox Text & SelectedIndex consistency / behavior

I have a piece of code where a ComboBox in DropDown mode on a form has as its datasource a fixed ArrayList of unique items; 我有一段代码,其中表单上的DropDown模式的ComboBox具有作为其数据源的唯一项的固定ArrayList; usually this works just fine for users, but very rarely, an error occurs where the SelectedIndex fails to the item corresponding to the item represented by the ComboBox text property entered by the user (even when it is a legitimate item). 通常这对用户来说效果很好,但很少发生错误,其中SelectedIndex对与用户输入的ComboBox文本属性所代表的项目相对应的项目失败(即使它是合法项目)。

To be more specific, a user types a legitimate entry as text in the ComboBox text field and navigates away to another control using the mouse. 更具体地说,用户在ComboBox文本字段中键入合法条目作为文本,并使用鼠标导航到另一个控件。 The text-field continues to show the entry but at some later point, when the user commits the changes using a method that depends on the SelectedIndex corresponding to the text being show, the SelectedIndex is incorrect (on occasion). 文本字段继续显示条目,但稍后,当用户使用依赖于与正在显示的文本对应的SelectedIndex的方法提交更改时,SelectedIndex不正确(有时)。

The official documentation http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.text.aspx states: 官方文档http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.text.aspx说明:

"Setting the Text property to null or an empty string ("") sets the SelectedIndex to -1. Setting the Text property to a value that is in the Items collection sets the SelectedIndex to the index of that item. Setting the Text property to a value that is not in the collection leaves the SelectedIndex unchanged." “将Text属性设置为null或空字符串(”“)将SelectedIndex设置为-1。将Text属性设置为Items集合中的值将SelectedIndex设置为该项目的索引。将Text属性设置为不在集合中的值会使SelectedIndex保持不变。“

So legitimate text entries should move the SelectedIndex to the proper number. 因此,合法的文本条目应该将SelectedIndex移动到正确的数字。 I know I can force a consistency check into an event handler before anything important happens, but I was wondering what could be causing this rare bug which flies in the face of the documentation to better understand what ComboBox is actually doing. 我知道我可以在发生任何重要事件之前强制对事件处理程序进行一致性检查,但我想知道是什么原因可能导致这种罕见的bug在文档面前飞行以更好地理解ComboBox实际上在做什么。 I'd appreciate any help. 我很感激任何帮助。

When the ComboBox is in DropDown Mode, the SelectedIndexChanged even is not fired when the user does not select from the items in the list. 当ComboBox处于DropDown模式时,当用户未从列表中的项目中进行选择时,不会触发SelectedIndexChanged。 Hence, when the user simply types an entry in and then navigates away using the mouose, the selectedIndex is not changed. 因此,当用户简单地键入条目然后使用mouose导航时,selectedIndex不会改变。 In order for this to work, you need to manually handle the ComboBox.Leave event such that any text typed in by the user is then selected. 为了使其工作,您需要手动处理ComboBox.Leave事件,以便随后选择用户键入的任何文本。

Private Sub ComboBox1_Leave(sender As Object, e As System.EventArgs) Handles ComboBox1.Leave
    ' This will cause the SelectedIndex to be changed, thus firing the Selected_IndexChanged Event:
    ComboBox1.SelectedIndex = ComboBox1.FindStringExact(ComboBox1.Text)
End Sub

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    MsgBox("SelectedIndex =" & ComboBox1.SelectedIndex.ToString())
End Sub

Right, after some testing this is my conclusion. 对,经过一些测试,这是我的结论。

When you are typing in the textfield of a combobox you are not actually triggering the Index selection. 当您在组合框的文本字段中键入时,实际上并未触发索引选择。 However, if you set the Text property to a value. 但是,如果将Text属性设置为值。 ie Combobox1.Text = "Existing item" then it sets the selected index. 即Combobox1.Text =“现有项目”然后它设置所选索引。 So it seems that you really shouldn't have any correct results in your database if they type instead of selecting in combobox. 因此,如果他们输入而不是在组合框中选择,那么您的数据库中似乎确实不应该有任何正确的结果。

Right, so this is where it gets stupid. 是的,所以这就是它变得愚蠢的地方。 This will work: 这将有效:

Private Sub ComboBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.TextChanged
    ComboBox1.Text = ComboBox1.Text
End Sub

I would however recommend you to do this instead: 不过我会建议你这样做:

Private Sub ComboBox1_Leave(sender As System.Object, e As System.EventArgs) Handles ComboBox1.Leave
    ComboBox1.Text = ComboBox1.Text
End Sub

To make my answer differ some I'll add this. 为了使我的答案有所不同,我将添加此内容。

If you add these settings for the combobox then typing will work without the leave event. 如果为组合框添加这些设置,则输入将在没有leave事件的情况下工作。

    ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
    ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM