VB.NET: how to prevent user input in a ComboBox

前端 未结 9 588
[愿得一人]
[愿得一人] 2021-02-03 17:05

How do you prevent user input in a ComboBox so that only one of the items in the defined list can be selected by the user?

相关标签:
9条回答
  • 2021-02-03 17:28

    Even if the question is marked answered, I would like to add some points to it.

    Set the DropDownStyle property of the combobox to DropDownList works for sure.

    BUT what if the drop down list is longer, the user will have to scroll it to the desired item as he has no access to keyboard.

     Private Sub cbostate_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles cbostate.Validating
        If cbostate.SelectedValue Is Nothing AndAlso cbostate.Text <> String.Empty Then
            e.Cancel = True
            MsgBox("Invalid State")
        End If
    End Sub
    

    I did it like this. I wanted to restrict the user entering 'random values' instead of 'state' but keeping he should be able to type and search states.

    This validating event occurs when the control loses focus. So if user enters wrong value in combobox, It will not allow user to do anything on the form, perhaps it will not even allow to change the focus from the combobox

    0 讨论(0)
  • 2021-02-03 17:28

    ---- in form level Declaration of cbx veriable---

    Dim cbx as string
    
    Private Sub comboBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comboBox1.Enter
        cbx = Me.comboBox1.Text
    End Sub
    
    Private Sub comboBox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comboBox1.Leave
        Me.comboBox1.Text = cbx
    End Sub
    
    0 讨论(0)
  • 2021-02-03 17:31

    Seeing a user banging away at a control that overrides her decisions is a sad sight. Set the control's Enabled property to False. If you don't like that then change its Items property so only one item is selectable.

    0 讨论(0)
  • 2021-02-03 17:34
    Private Sub ComboBox4_KeyPress(sender As Object, e As KeyPressEventArgs) Handles ComboBox4.KeyPress
        e.keyChar = string.empty
    End Sub
    
    0 讨论(0)
  • 2021-02-03 17:36

    Use KeyPressEventArgs,

    Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
        e.Handled = True
    End Sub
    
    0 讨论(0)
  • 2021-02-03 17:40

    Using 'DropDownList' for the 'DropDownStyle' property of the combo box doesn't work as it changes the look and feel of the combo box. I am using Visual Studio 2019 Community Edition.

    Using 'e.Handled = True' also doesn't work either as it still allows user input. Using 'False' for the 'Enabled' property of the combo box also doesn't work too because it stops the user from being able to use the combo box.

    All of the "solutions" that have been proposed above are complete rubbish. What really works is this following code which restricts user input to certain keys such as up / down (for navigating through the list of all available choices in the combo box), suppressing all other key inputs:-

    Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles 
      ComboBox1.KeyDown
        REM The following code is executed whenever the user has pressed
        REM a key.  Added on Wednesday 1st January 2020.
    
        REM Determine what key the user has pressed.  Added on Wednesday
        REM 1st January 2020.
        Select Case e.KeyCode
            Case Keys.Down, Keys.Up
                REM This section is for whenever the user has pressed either
                REM the arrow down key or arrow up key.  Added on Wednesday
                REM 1st January 2020.
    
            Case Else
                REM This section is for whenever the user has pressed any
                REM other key.  Added on Wednesday 1st January 2020.
    
                REM Cancelling the key that the user has pressed.  Added on
                REM Wednesday 1st January 2020.
                e.SuppressKeyPress = True
    
        End Select
    
    End Sub
    
    0 讨论(0)
提交回复
热议问题