7

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

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

如果我单击下拉箭头combobox并单击我想要的项目,SelectedIndexChanged则称为 - 好。

如果我在文本区域内单击combobox并开始输入我想要选择的内容并通过按向上(或向下)键完成它,SelectedIndexChanged则称为 - 也很好。

如果我单击下拉箭头combobox并开始输入我想要选择的内容并按 ENTER 完成它,SelectedIndexChanged则不会被称为 - 问题。

是否存在由最后一种情况引起的不同事件ENTER?我尝试使用TextChangedandTextUpdate事件,但这些似乎不起作用:

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”。

4

9 回答 9

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;
        }
    }
}

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

编辑:

为防止用户不得不键入整个字符串,请使用 Adi 答案中的建议:转到组合框的属性并设置AutoCompleteModeSuggestAppendAutoCompleteSource设置ListItems- 我在创建答案时实际上使用了这些设置,所以它应该适合你.

于 2012-12-15T07:49:49.543 回答
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
于 2012-12-15T08:24:09.200 回答
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
于 2012-12-19T03:14:27.663 回答
3

我相信你应该设置AutoCompleteModetoSuggestAppendAutoCompleteSourceto ListItems。这样,如果您输入的内容对于 ComboBox 中加载的项目是可持续的,通过写下来并找到该项目,当按下时EnterSelectedIndexChanged将被触发(即使找不到匹配项 - 列表中的第一个将被选中)

我已经准备了一些东西来向你指出这一点。

问候,

阿迪康斯坦丁

于 2012-12-18T08:53:32.977 回答
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
于 2012-10-05T17:05:03.913 回答
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
于 2012-10-09T04:24:09.140 回答
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
于 2012-12-17T01:13:09.670 回答
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

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

于 2014-11-30T06:52:48.803 回答
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
于 2014-03-28T11:18:05.530 回答