【问题标题】:Visual Studio ComboBox won't cycle through items with up/down arrows once users types一旦用户键入,Visual Studio ComboBox 将不会循环浏览带有向上/向下箭头的项目
【发布时间】:2016-12-09 07:35:57
【问题描述】:

我有一个带有下拉组合框的用户表单,该下拉组合框在打开表单时填充了项目。如果框有焦点,向上/向下箭头键将在项目之间循环(应该如此),或者用户可以从下拉列表中进行选择,并且仍然可以从那里使用向上/向下箭头。如果用户开始键入,则该框设置为从列表项中追加,但这会禁用向上/向下箭头键循环浏览所有项目。此时,它只会循环浏览以用户键入的任何内容开头的项目。我希望它像在 VBA 版本中一样擦除键入的数据并循环遍历所有组合框项目(从索引开始是附加的任何内容)。

即使用户输入了某些内容,然后从下拉列表中选择了完全不同的项目,下次按下上/下键时,它也只会返回输入的内容。

我尝试在向上/向下键 keydown(或 keyup)事件中处理此问题,如下所示:

Private Sub SeriesBox_KeyDown(sender As Object, e As KeyEventArgs) Handles SeriesBox.KeyDown

    Dim CurIndex As Integer = SeriesBox.FindStringExact(SeriesBox.Text)
    Select Case e.KeyCode

        Case Keys.Down

            SeriesBox.Text = String.Empty
            SeriesBox.SelectedIndex = CurIndex + 1

        Case Keys.Up

            SeriesBox.Text = String.Empty
            SeriesBox.SelectedIndex = CurIndex - 1

    End Select

End Sub

方向键实际上并没有进入这个sub,所以我尝试添加:

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keydata As Keys) As Boolean

  If keydata = Keys.Right Or keydata = Keys.Left Or keydata = Keys.Up Or keydata = Keys.Down Then
    OnKeyDown(New KeyEventArgs(keydata))
    ProcessCmdKey = True
  Else
    ProcessCmdKey = MyBase.ProcessCmdKey(msg, keydata)
  End If

End Function

但这似乎并没有什么不同。有什么我遗漏的吗?

【问题讨论】:

  • 不能使用If语句吗? If combobox_text = "" Then arrowkey_keypress does x, else arrowkey_keypress jumps to first appended value and goes only to appended values我知道这是伪代码,但值得一试,取决于你的VB经验水平

标签: vb.net combobox visual-studio-2015


【解决方案1】:

这可能有效:

首先将组合框的 AutoCompleteModeAutoCompleteSouce 设置为 None

然后将其粘贴到您的代码中:

 Private Sub SeriesBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles SeriesBox.TextChanged
    For Each it As String In SeriesBox.Items
        If Not SeriesBox.Text = it Then
            If it.Contains(SeriesBox.Text) Then
                Dim length As Integer = SeriesBox.Text.Length
                SeriesBox.Text = ""
                SeriesBox.Text = it
                SeriesBox.Select(length, SeriesBox.Text.Length - length)
                SeriesBox.SelectedItem = SeriesBox.Text
            End If
        End If
    Next
End Sub

【讨论】:

  • 感谢您提供出色的解决方案!我没想过只是手动处理自动完成。实际上,我确实必须稍微修改您的代码,以便它只能找到以输入内容开始的项目。我将“If it.contains...”更改为“If Left(it, Len(SeriesBox.Text)) = SeriesBox.Text”
  • 对不起,我没有注意到 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-15
  • 2018-10-23
  • 1970-01-01
  • 2010-09-21
  • 2016-11-12
  • 2022-08-12
相关资源
最近更新 更多