VB.NET: how to prevent user input in a ComboBox
How do you prevent user input in a ComboBox so that only one of the items in the defined lis开发者_Go百科t can be selected by the user?
Set the DropDownStyle
property of the combobox to DropDownList
. This will allow only items in the list to be selected and will not allow any free-form user input.
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
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.
Set the ReadOnly attribute to true.
Or if you want the combobox to appear and display the list of "available" values, you could handle the ValueChanged event and force it back to your immutable value.
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
this is the most simple way but it works for me with a ComboBox1 name
SOLUTION on 3 Basic STEPS:
step 1.
Declare a variable at the beginning of your form which will hold the original text value of the ComboBox. Example:
Dim xCurrentTextValue as string
step 2.
Create the event combobox1 key down and Assign to xCurrentTextValue variable the current text of the combobox if any key diferrent than "ENTER" is pressed the combobox text value keeps the original text value
Example:
Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
xCurrentTextValue = ComboBox1.Text
If e.KeyCode <> Keys.Enter Then
Me.ComboBox1.Text = xCmbItem
End If
End Sub
step 3.
Validate the when the combo text is changed if len(xcurrenttextvalue)> 0 or is different than nothing then the combobox1 takes the xcurrenttextvalue variable value
Private Sub ComboBox1_TextChanged(sender As Object, e As EventArgs) Handles ComboBox1.TextChanged
If Len(xCurrentTextValue) > 0 Then
Me.ComboBox1.Text = xCurrentTextValue
End If
End Sub
========================================================== that's it,
Originally I only tried the step number 2, but I have problems when you press the DEL key and DOWN arrow key, also for some reason it didn't validate the keydown event unless I display any message box
!Sorry, this is a correction on step number 2, I forgot to change the variable xCmbItem to xCurrentTextValue, xCmbItem it was used for my personal use
THIS IS THE CORRECT CODE
xCurrentTextValue = ComboBox1.Text
If e.KeyCode <> Keys.Enter Then
Me.ComboBox1.Text = xCurrentTextValue
End If
---- 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
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
Private Sub ComboBox4_KeyPress(sender As Object, e As KeyPressEventArgs) Handles ComboBox4.KeyPress
e.keyChar = string.empty
End Sub
精彩评论