Vb.net 组合框';s SelectedIndexChanged事件未在Enter时调用

Vb.net 组合框';s SelectedIndexChanged事件未在Enter时调用,vb.net,Vb.net,我正在用VB使用.NETFramework4.0开发VS2010 我有一个组合框。它有一些项目,并显示刚刚好。这里有点奇怪: 如果我单击组合框上的下拉箭头并单击所需的项目,SelectedIndexChanged被称为-good 如果我在组合框的文本区域内单击,并开始键入我想要选择的内容,然后按向上(或向下)键完成,则调用SelectedIndexChanged 如果单击组合框上的下拉箭头,开始键入所需的选定内容,然后按ENTER键完成,则不会调用SelectedIndexChanged-问题

我正在用VB使用.NETFramework4.0开发VS2010

我有一个
组合框
。它有一些项目,并显示刚刚好。这里有点奇怪:

如果我单击组合框
上的下拉箭头
并单击所需的项目,
SelectedIndexChanged
被称为-good

如果我在
组合框的文本区域内单击,并开始键入我想要选择的内容,然后按向上(或向下)键完成,则调用
SelectedIndexChanged

如果单击组合框上的下拉箭头
,开始键入所需的选定内容,然后按ENTER键完成,则不会调用SelectedIndexChanged-问题

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

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(key.Enter)
之外的东西吗

我还应该找别的活动吗

编辑:
组合框中的项目示例如下:

  • 10-新条目和完整性检查
    -->这是最常见的类型
  • 13-分配给TRB/HRB
    -->有一些带有“/”
  • 60-外部(等待进一步通知)
    -->有一些带有“(”和“)”

基本上,每个列表的类型是“##-一些文本”。

订阅按键事件:

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

这将有助于解决你的问题

 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

免责声明:这是用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答案中的建议:转到组合框的属性,将set
AutoCompleteMode
设置为
SuggestAppend
AutoCompleteSource
设置为
ListItems
——我在创建答案时实际使用了这些设置,所以它应该对你有用。

你能告诉我这对你有用吗

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

我认为您应该将
自动完成模式
设置为
SuggestAppend
并将
自动完成源
设置为
列表项
。这样,如果您键入的内容与组合框中加载的项目是可持续的,则通过将其写下来并找到该项目,当按Enter键时,将触发
SelectedIndexChanged
(即使找不到匹配项,也会选择列表中的第一个)

我准备向你指出这一点

问候,


Adi Konstantin

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

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
Private Sub cmbStatus_PreviewKeyDown(sender As ComboBox, e As PreviewKeyDownEventArgs) Handles cmbStatus.PreviewKeyDown
    If e.KeyCode = Keys.Enter Then sender.DroppedDown = False
End Sub

当combobox dropdownstyle设置为simple,而autocomplete设置为append时,我也遇到了类似的问题,源于我的listitems。我的解决方法是在每次textchanged事件时使用字符串变量保存combobox文本。这样,当按下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
“全局声明”
将所选文本设置为字符串=“”
私有子combobox_textchanged(发送者作为对象,e作为事件参数)处理combobox.textchanged
如果combobox.Text不是Nothing,而combobox.Text是“”,则
所选文本=组合框。文本
如果结束
端接头
私有子组合框\u keydown(发送方作为对象,e作为KeyEventArgs)处理combobox.keydown
如果e.KeyCode=Keys,则输入
combobox.Text=所选文本
“把逻辑放在这里
如果结束
端接头

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

这有点接近;正在调用
KeyDown
事件,但实际上它不会更改
ComboBox
的索引。实际上,您需要什么,请告诉我详细信息……在上述代码中,如果您在ComboBox的特定索引中按enter键,您可以在键事件内执行函数。问题是,键入值并更改选定索引时,不会调用SelectedIndexChanged。为什么?我很感激这一点,但因为我不希望用户必须在组合框中键入整个字符串才能找到匹配项(有些字符串超过20个字符),所以我不需要这样做。不过这段话很好。谢谢嘎,真的很接近了。好的,这确实使它认识到索引有变化。但是,无论我键入什么(无论我希望它指向哪个索引),在回车时它都会被设置为列表中的第一项。它在文本上看起来不错,但是当它到达If((index=comboBox1.FindStringExact(comboBox1.text))-1)
时,它只是返回
1
。。。找不到字符串的索引…@redhospike您能用组合框中实际包含的项目更新您的问题吗?另外,请确保comboBox1在代码中是正确的名称。我已编辑了我的问题,以包含ComboxBox项的示例。这显然更接近我所需要的内容-除非找到匹配项,并且用户单击enter,
组合框
中的文本已清除…有什么建议吗?这与@AdiKonstantin的建议配合使用会很好,但当用户在找到匹配项后按enter键时,文本将被清除。你对此有什么建议吗?谢谢,但结果与@Sean Vaugh的建议相同。这太棒了!哇!它基本上起作用了。唯一的问题是如果