如何防止用户在ComboBox中输入,以便只有定义列表中的一个项目可以被用户选择?
将组合框的DropDownStyle
属性设置为DropDownList
。这将仅允许选择列表中的项目,并不允许任何自由形式的用户输入。
Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
e.Handled = True
End Sub
看到用户一遍又一遍地敲打一个覆盖她决策的控件是很令人悲伤的。将该控件的Enabled属性设为False。如果您不喜欢这样做,可以更改它的Items属性,使其只能选择一个项目。
即使问题被标记为已回答
,我仍然想要添加一些观点
。
将组合框的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
这是最简单的方法,但对我来说,它适用于名为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
使用“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
----在表单级别声明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
Private Sub ComboBox4_KeyPress(sender As Object, e As KeyPressEventArgs) Handles ComboBox4.KeyPress
e.keyChar = string.empty
End Sub