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

65

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

9个回答

137

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


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

1
我更喜欢这个,因为我仍然可以在我的组合框中添加占位符文本;将“DropDownStyle”更改为“DropDownList”将删除它上面的默认文本。 - Curbside Coder
不太适合那些想要使用键盘选择项目的人。 - Ry-
和 @CurbsideCoder 相同的原因。 - danielpr

2

看到用户一遍又一遍地敲打一个覆盖她决策的控件是很令人悲伤的。将该控件的Enabled属性设为False。如果您不喜欢这样做,可以更改它的Items属性,使其只能选择一个项目。


0

即使问题被标记为已回答,我仍然想要添加一些观点

将组合框的DropDownStyle属性设置为DropDownList肯定有效。

但是,如果下拉列表很长,用户将不得不滚动它以找到所需的项目,因为他无法使用键盘。

 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

我是这样做的。 我希望限制用户输入“随机值”而不是“状态”,但是仍然应该可以键入和搜索状态。 当控件失去焦点时,会发生此“验证事件”。 因此,如果用户在组合框中输入错误的值,则它将不允许用户在表单上执行任何操作,甚至可能不允许更改组合框的焦点。

0
将ReadOnly属性设置为true。
或者,如果您希望组合框显示并显示“可用”值列表,则可以处理ValueChanged事件并将其强制设置回不可变值。

-1

这是最简单的方法,但对我来说,它适用于名为ComboBox1的组合框。

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

第一步。

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

     Dim xCurrentTextValue as string

步骤2。

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

例子:

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

步骤三。

当组合框文本被更改时,验证以下内容: 如果len(xcurrenttextvalue)> 0或不等于nothing,则combobox1采用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

========================================================== 就是这样了,

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


抱歉,这是关于第二步的更正,我忘记将变量 xCmbItem 更改为 xCurrentTextValue,xCmbItem 是我个人使用的

这是正确的代码

xCurrentTextValue = ComboBox1.Text

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

-1

使用“DropDownList”作为组合框的“DropDownStyle”属性不起作用,因为它会改变组合框的外观和感觉。我正在使用Visual Studio 2019 Community Edition。

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

所有上面提出的“解决方案”都是完全无用的。真正有效的是以下代码,它限制了用户输入某些键,例如向上/向下(用于浏览组合框中所有可用选择的列表),抑制所有其他键输入:

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

键不是将输入传递到ComboBox的唯一方式,自由输入也不是非箭头键唯一用途。DropdownList样式是正确的答案(当你试图打破该功能时拥有自由输入ComboBox外观和感觉是错误的设计 - 这解决方案并未允许搜索)。 - Ry-
我认为你对外观和感觉确实有一点道理。如果你能改进语言,并且可能将某些句子相对化,我认为你的答案会更有价值。尽管如此,我理解你的挫败感。 - DarkTrick

-1

----在表单级别声明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

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

e.keychar = string.empty - user6051640
1
请添加更多信息。仅包含代码和“试试这个”答案是不被鼓励的(因为它们不包含可搜索的内容,也没有解释为什么应该“试试这个”)。我们在这里努力成为知识资源。 - Mogsdad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接