VB.NET:如何防止用户在 ComboBox 中输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2773430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:14:48  来源:igfitidea点击:

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

.netvb.netcombobox

提问by CJ7

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?

如何防止用户在 ComboBox 中输入,以便用户只能选择定义列表中的一项?

回答by DCNYAM

Set the DropDownStyleproperty 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.

DropDownStyle组合框的属性设置为DropDownList。这将只允许选择列表中的项目,并且不允许任何自由格式的用户输入。

回答by w4hy03

Use KeyPressEventArgs,

使用 KeyPressEventArgs,

Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
    e.Handled = True
End Sub

回答by Hans Passant

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.

看到用户猛烈抨击凌驾于她的决定的控件是一种悲伤的景象。将控件的 Enabled 属性设置为 False。如果您不喜欢那样,请更改其 Items 属性,以便只能选择一项。

回答by Ax.

Set the ReadOnly attribute to true.

将 ReadOnly 属性设置为 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.

或者,如果您希望组合框出现并显示“可用”值列表,您可以处理 ValueChanged 事件并将其强制返回到您的不可变值。

回答by Shreekant

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

即使问题是marked answered,我也愿意add some points

Set the DropDownStyleproperty of the combobox to DropDownListworks for sure.

DropDownStyle组合框的属性设置DropDownList为确实有效。

BUTwhat 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 eventoccurs when the control loses focus. So if user enters wrong valuein combobox, It will not allow userto do anything on the form, perhaps it will not even allow to change the focus from the combobox

validating event当控制丢失时会发生这种情况focus。因此,如果用户输入wrong valuecombobox,它会not allow user做任何事情的形式,也许它甚至不会允许将焦点从组合框改变

回答by rgvpctech

this is the most simple way but it works for me with a ComboBox1 name

这是最简单的方法,但它适用于我的 ComboBox1 名称

SOLUTION on 3 Basic STEPS:

关于 3 个基本步骤的解决方案:

step 1.

第1步。

Declare a variable at the beginning of your form which will hold the original text value of the ComboBox. Example:

在表单的开头声明一个变量,该变量将保存 ComboBox 的原始文本值。例子:

     Dim xCurrentTextValue as string

step 2.

第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

创建事件组合框 1 键并将组合框的当前文本分配给 xCurrentTextValue 变量,如果按下与“ENTER”不同的任何键,组合框文本值保持原始文本值

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.

第 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

如果 len(xcurrenttextvalue)> 0 或与没有不同,则验证组合文本何时更改,则组合框 1 采用 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

========================================================== 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

最初我只尝试了第 2 步,但是当你按下 DEL 键和向下箭头键时我遇到了问题,而且由于某种原因它没有验证 keydown 事件,除非我显示任何消息框



!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

!对不起,这是对第 2 步的更正,我忘记将变量 xCmbItem 更改为 xCurrentTextValue,xCmbItem 是我个人使用的

THIS IS THE CORRECT CODE

这是正确的代码

xCurrentTextValue = ComboBox1.Text

If e.KeyCode <> Keys.Enter Then
    Me.ComboBox1.Text = xCurrentTextValue
End If

回答by Ahmed

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

---- 在表单级别声明 cbx 可验证---

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

回答by Chronic Clawtooth

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.

将“DropDownList”用于组合框的“DropDownStyle”属性不起作用,因为它会改变组合框的外观。我正在使用 Visual Studio 2019 社区版。

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.

使用 'e.Handled = True' 也不起作用,因为它仍然允许用户输入。对组合框的 'Enabled' 属性使用 'False' 也不起作用,因为它阻止用户使用组合框。

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

回答by user6051640

Private Sub ComboBox4_KeyPress(sender As Object, e As KeyPressEventArgs) Handles ComboBox4.KeyPress
    e.keyChar = string.empty
End Sub