在Enter上没有调用ComboBox的SelectedIndexChanged事件

时间:2012-10-05 16:23:38

标签: vb.net

我正在使用.NET Framework 4.0使用VB开发VS 2010

我有combobox。它有一些项目,显示得很好。这里有点奇怪:

如果我点击combobox上的下拉箭头并点击我想要的项目,则会调用SelectedIndexChanged - 很好。

如果我在combobox的文本区域内单击并开始键入我想要的内容并按向上(或向下)键完成,则调用SelectedIndexChanged - 也很好。

如果我点击combobox上的下拉箭头并开始输入我想要的内容并按ENTER键完成,则不会调用SelectedIndexChanged - 问题。

在最后一种情况下,ENTER会导致不同的事件吗?我已尝试使用TextChangedTextUpdate事件,但这些事件似乎不起作用:

Private Sub cmbStatus_TextChanged(sender As System.Object, e As System.EventArgs) Handles cmbStatus.TextChanged
If e.Equals(Keys.Enter) Then
    Call SomeMethod()
End If

我应该使用e.Equals(Keys.Enter)以外的其他内容吗?

我还应该寻找其他事件吗?

修改 ComboBox中的项目示例如下:

  • 10 - 新进入和完成检查---> this is the most common type
  • 13 - 分配给TRB / HRB ---> there are a few with '/'
  • 60 - 外部(持续进一步通知)---> there are a few with '(' and ')'

基本上,每个列表的类型是“## - SOME TEXT”。

9 个答案:

答案 0 :(得分:6)

免责声明:这是用C#编写的 - 如果您需要将其翻译成VB,请告诉我。

private void comboBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    //It's important to also check that the Combo Box is displaying its Drop Down. If
    //you want this to execute even when it is not displayed, remove the check for
    //comboBox1.DroppedDown.
    if (e.KeyCode == Keys.Enter && comboBox1.DroppedDown &&
        !string.IsNullOrEmpty(comboBox1.Text))
    {
        int index;
        //Attempt to locate the string typed in by the user. An index of -1
        //means it was not found.
        if ((index = comboBox1.FindStringExact(comboBox1.Text)) != -1)
        {
            //Update the SelectedIndex.
            comboBox1.SelectedIndex = index;
        }
    }
}

有趣的是,docs表示在处理用户所做的选择更改时,我们应该使用SelectionChangeCommitted事件而不是SelectedIndexChanged事件。在这种情况下有必要这样做,因为SelectedIndexChanged事件使用我的方法触发两次。

修改

要防止用户输入整个字符串,请使用Adi答案中的建议:转到组合框的属性并将AutoCompleteMode设置为SuggestAppendAutoCompleteSourceListItems - 我在创建答案时实际使用了这些设置,因此它应该适合你。

答案 1 :(得分:4)

Private Sub ComboBox1_KeyUp(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyUp
    If e.KeyCode = Keys.Enter Then
        MessageBox.Show(ComboBox1.SelectedText)
        Call SomeMethod()
    End If
End Sub

答案 2 :(得分:4)

Option Strict On
Public Class Form1
    Friend WithEvents ComboBox1 As New ComboBox With {.Parent = Me}
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        ComboBox1.Items.AddRange({"hello", "tes1ted", "word", "item", "tes2ted"})
        ComboBox1.Text = ComboBox1.Items(0).ToString
    End Sub
    Private Sub ComboBox1_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs) Handles ComboBox1.KeyUp
        'You can put this in the keydown event, or adapt it a small bit and put it in the keypress event
        'putting it in the textchanged event is problematic and not recommended.
        Dim OriginalText As String = ComboBox1.Text
        If e.KeyCode = Keys.Enter Then
            If ComboBox1.SelectionLength > 0 Then
                ComboBox1.Text = ComboBox1.Text
                ComboBox1.SelectionLength = 0
                ComboBox1.SelectionStart = ComboBox1.Text.Length
            End If
        End If
        If Not IsTextKey(e.KeyCode) Then Exit Sub
        Dim Filter As String = ComboBox1.Text & "*"
        If Filter.Length = 1 Then Exit Sub
        For I = 0 To ComboBox1.Items.Count - 1
            If LCase(ComboBox1.Items(I).ToString) Like LCase(Filter) Then
                ComboBox1.SelectedItem = ComboBox1.Items(I)
                ComboBox1.Select(OriginalText.Length, (ComboBox1.Text.Length - OriginalText.Length))
                Exit Sub
            End If
        Next
    End Sub
    Function IsTextKey(ByVal Key As Integer) As Boolean
        Select Case True
            Case Key = Keys.Up : Return False
            Case Key = Keys.Down : Return False
            Case Key = Keys.Left : Return False
            Case Key = Keys.Right : Return False
            Case Key = Keys.Back : Return False
            Case Key = Keys.Delete : Return False
            Case Key = Keys.LWin : Return False
            Case Key = Keys.RWin : Return False
                'add whatever I missed
                'return false if the key either removes text from the textbox 
                'or does not produce a character
            Case Else
                'return true if the key produces a visible character(including space)
                Return True
        End Select
    End Function
End Class

答案 3 :(得分:3)

我认为您应该将AutoCompleteMode设置为 SuggestAppend ,将AutoCompleteSource设置为 ListItems 。这样,如果您输入的内容与ComboBox中加载的项目一致,通过写下来并找到该项目,当按下 Enter 时,SelectedIndexChanged将被触发(即使找不到匹配项 - 列表中的第一个将被选中)

我已准备好something向你指出这一点。

此致

Adi Konstantin

答案 4 :(得分:2)

订阅KeyPressed活动:

Private Sub yourComboBox_KeyPressed(sender As System.Object, e As System.KeyPressedEventArgs) Handles yourComboBox.KeyPressed
    If e.KeyChar.Equals((char)Keys.Enter) Then
        Call SomeMethod()
End If

答案 5 :(得分:2)

这将有助于解决您的问题

 Private Sub ComboBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
    If e.KeyCode = Keys.Enter Then
        MsgBox("hello")'call some functions
    End If
End Sub

答案 6 :(得分:2)

如果这对你有用,你能告诉我吗?

    Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
      If e.KeyChar = Chr(13) Then
        ComboBox1_SelectedIndexChanged(sender, e)
      End If
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
      MsgBox(ComboBox1.Text)
      'Your code here
    End Sub

答案 7 :(得分:2)

当组合框下拉式设置为简单时,自动完成设置为追加,从我的列表项中获取,我遇到了类似的问题。我的解决方法是在每个textchanged事件上都有一个字符串变量保存组合框文本。这样,当按下ENTER时,您可以检索已删除的文本并将其重新分配给组合框文本。

'全球声明

'Global declaraion
dim selected_text as string = ""

 Private Sub combobox_textchanged(sender As Object, e As EventArgs) Handles combobox.TextChanged
        If combobox.Text IsNot Nothing And combobox.Text <> "" Then
            selected_text = combobox.Text
        End If
 End Sub

Private Sub combobox_keydown(sender As Object, e As KeyEventArgs) Handles combobox.KeyDown
        If e.KeyCode = Keys.Enter Then
            combobox.Text = selected_text
            'put logic here
        End If
End Sub

上面有一些很好的答案,但我喜欢我不需要整理所有的命令键和箭头等。

答案 8 :(得分:0)

在PreviewKeyDown事件上手动关闭列表:

Private Sub cmbStatus_PreviewKeyDown(sender As ComboBox, e As PreviewKeyDownEventArgs) Handles cmbStatus.PreviewKeyDown
    If e.KeyCode = Keys.Enter Then sender.DroppedDown = False
End Sub