使用DropdownStyle = DropDownList忽略下拉菜单中的输入按键

时间:2019-01-14 05:13:22

标签: vb.net winforms

我有一个组合框,用户可以键入该组合框以过滤出结果。

要实现此目的,我将dropDownStyle设置为DropdownList并按如下所示设置KeyUp事件

Private Sub cbRPP_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs) Handles cbRPP.KeyUp
    If boredUserProtection Then Return

    If e.KeyCode = Keys.Enter Then
        e.SuppressKeyPress = True
        e.Handled = True
        Return 'Boss doesn't want enter to do anything
    End If
    ComboKeyPressed()
End Sub


Private Sub cbRPP_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles cbRPP.SelectionChangeCommitted
    If boredUserProtection Then Return 'boredUserProtection = a flag to prevent the user from typing into the box once they've entered some text
    Try
        boredUserProtection = True
        menuButton.Select()
        TooltipNotify("Loading")
        cbRPP.Text = String.Empty
        LoadRegion()
        menuButton.Select()
    Catch ex As Exception
        ErrorReporter.Program.ReportError(ex, "RPPCalibator", "Handled Gracefully")
    Finally
        boredUserProtection = False
    End Try
End Sub

问题是我的老板现在要求我阻止按Enter键时发生任何动作。

我试图通过拦截按键并抑制是否输入来实现这一点,但是当dropdownStyle = dropdownList

时似乎不起作用
Private Sub cbRPP_KeyPress(sender As Object, e As KeyPressEventArgs) Handles cbRPP.KeyPress
    If e.KeyChar = vbCrLf Then
        e.Handled = True
        Return 'Boss doesn't want enter to do anything
    End If
End Sub

Private Sub cbRPP_KeyDown(sender As Object, e As KeyEventArgs) Handles cbRPP.KeyDown
    If e.KeyCode = Keys.Enter Then
        e.SuppressKeyPress = True
        e.Handled = True
        Return 'Boss doesn't want enter to do anything
    End If
End Sub

我只是想知道是否有人可以阐明我如何防止在输入keyprss时发生任何事件,因为我可以在Google上轻易找到的一条建议(拦截KeyDown或KeyUp)似乎不起作用

(我是一名实用主义者,因此,无论您采用哪种解决方案,都将不胜感激)

0 个答案:

没有答案