VB.NET: how to prevent user input in ComboBox

How do you prevent a user from entering ComboBox so that the user can select only one of the items in a specific list?

+46
combobox
May 05 '10 at 13:08
source share
10 answers

Set the DropDownStyle property of this field to map the DropDownList . This will allow you to select only the items in the list and prevent the user from entering a free form.

+102
May 05 '10 at 13:15
source share

Seeing how a user strikes a control that overrides its decisions is a sad sight. Set the Enabled property to False. If you do not like this, change its Items property so that only one item can be selected.

+2
May 05 '10 at 13:44
source share

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 
+2
Feb 18 '14 at 15:16
source share

Make Combobox Readonly. In this case, the User cannot enter his text or change the data.

STEPS:

  • Select your combo box.
  • Go to your property list and select DropdownStyle and change its value to DropdownList.
+1
Jan 11 '13 at 16:14
source share

Set the ReadOnly attribute to true.

If you want the dropdown to display and display a list of available values, you can handle the ValueChanged event and return it back to the immutable value.

0
May 05 '10 at 13:12
source share

this is the easiest way, but it works for me with the name ComboBox1

SOLUTION on 3 main elements:

step 1.

Declare a variable at the beginning of the form that will contain the original text value of the ComboBox. Example:

  Dim xCurrentTextValue as string 

Step 2

Create a combobox1 key down event and set xCurrentTextValue to the current combobox text; if a key other than "ENTER" is pressed, the combobox text value retains 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

Confirm when the combined text changes if len (xcurrenttextvalue)> 0 or differs from nothing, then combobox1 takes the value of the variable xcurrenttextvalue

 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 

==================================================== ======== what is he,

Initially, I only tried step number 2, but I have problems when you press the DEL key and the down arrow, also for some reason it did not confirm the keydown event if I do not show any message box




! Sorry, this fix is ​​in step number 2, I forgot to change the xCmbItem variable to xCurrentTextValue, xCmbItem, which was used for my personal use

THIS IS THE RIGHT CODE

 xCurrentTextValue = ComboBox1.Text If e.KeyCode <> Keys.Enter Then Me.ComboBox1.Text = xCurrentTextValue End If 
0
May 18 '13 at 5:15
source share

---- at the form level cbx veriable declaration ---

 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
Apr 05 '17 at 5:02 on
source share

I fix the format - Thanks

---- at the form level Declaration cbx veriable --- dim cbx as a 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
Apr 05 '17 at 5:05 on
source share
 Private Sub ComboBox4_KeyPress(sender As Object, e As KeyPressEventArgs) Handles ComboBox4.KeyPress e.keyChar = string.empty End Sub 
-one
Mar 11 '16 at 19:29
source share

I think this is the correct and easiest code for this kind of problem.

 Private Sub CourseName_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles CourseName.KeyPress e.Handled = True End Sub 
-one
Nov 24 '17 at 12:29
source share



All Articles