vb.net 仅从组合框中选择列表项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14981204/
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
Only select list items from combobox
提问by Hemal Rathod
I have a winforms application in visual studio 2010.
我在 Visual Studio 2010 中有一个 winforms 应用程序。
On a form, I have a databound combobox, for which i have set autocompletesource=listitems and autocompletemode=suggestappend.
在表单上,我有一个数据绑定组合框,为此我设置了 autocompletesource=listitems 和 autocompletemode=suggestappend。
Now For this functionality to work, I have set dropdownstyle=dropdown, so that user can type a text
现在为了让这个功能起作用,我设置了 dropdownstyle=dropdown,以便用户可以输入文本
But I want a user to be able to select only an item available from its dropdown.
但我希望用户只能从其下拉列表中选择一个可用的项目。
If user enters item other than list items, and leaves combobox, user should not be able to leave combobox.
如果用户输入列表项以外的项目,并离开组合框,用户应该无法离开组合框。
In short, I want user to be able to select item only from available listitems, not anything he enters.
简而言之,我希望用户只能从可用列表项中选择项目,而不是他输入的任何内容。
plz help
请帮忙
回答by Steven Doggart
If you set DropDownStyle = DropDownList
and AutoCompleteMode = Append
, the user will still be able to type the value to select the item they want, but they will be limited to the items that are in the list.
如果您设置DropDownStyle = DropDownList
和AutoCompleteMode = Append
,用户仍然可以输入值来选择他们想要的项目,但他们将被限制为列表中的项目。
When AutoCompleteMode = Append
, it will check subsequent characters typed by appending them to the value being searched, as long as you type them quickly, that is. If you wait too long between key strokes, then it will go back to the first letter search again.
当 时AutoCompleteMode = Append
,它会通过将它们附加到正在搜索的值来检查输入的后续字符,只要您快速输入它们,即。如果您在击键之间等待太长时间,那么它将再次返回到第一个字母搜索。
Consider: do you really need them to be able to enter an invalid value just so you can alert them that it's invalid? Because if not, it's just more confusing that way. By giving them the opportunity to enter any value, it implies that they are allowed to do so.
考虑一下:您是否真的需要他们能够输入无效值,以便您可以提醒他们这是无效的?因为如果没有,那只会更令人困惑。通过让他们有机会输入任何值,这意味着他们被允许这样做。
回答by Cubsoft
Set the property 'DropDownStyle' to 'DropdownList' and this will stop the user from typing into the combo.
将属性“DropDownStyle”设置为“DropdownList”,这将阻止用户输入组合。
Hope this helps.
希望这可以帮助。
回答by FreezeGhost
I look for some solution but without using limiting DropDownList (typing is time limited users must be quick).
我寻找一些解决方案,但不使用限制 DropDownList(打字时间有限,用户必须快速)。
Previous code seems good for me, but is not called during typing what we required. ComboBox I switch to AutoCompleteMode = SuggestAppend
, AutoCompleteSource = ListItems
, DoprDownStyle = DropDown
. This allow user directly typing to box and is not time limited.
以前的代码对我来说似乎不错,但在键入我们需要的内容时不会调用。组合框我切换到AutoCompleteMode = SuggestAppend
, AutoCompleteSource = ListItems
, DoprDownStyle = DropDown
。这允许用户直接输入框并且不受时间限制。
This is my code I hope will help to some one:
这是我的代码,希望对某些人有所帮助:
Private Sub ComboBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyUp
If ComboBox1.Text <> String.Empty Then
If ComboBox1.FindString(cboSkupina.Text) = -1 Then 'if is value -1 typed text is not in list
ComboBox1.Text = Mid(ComboBox1.Text, 1, Len(ComboBox1.Text) - 1) 'Delete not valid character
ComboBox1.SelectionStart = Len(ComboBox1.Text) + 1 'Place cursor at the end
End If
End If
End Sub
回答by Laxmikant Bhumkar
Just add the following snippet to KeyPress eventof ComboBox. Remember to replace Combobox name with yours one.
只需将以下代码段添加到ComboBox 的KeyPress 事件中即可。请记住将 Combobox 名称替换为您的名称。
Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
If e.KeyChar = ControlChars.Back AndAlso e.KeyChar = ControlChars.Back Then
Return
End If
Dim t As String = ComboBox1.Text
Dim typedT As String = t.Substring(0, ComboBox1.SelectionStart)
Dim newT As String = typedT + e.KeyChar
Dim i As Integer = ComboBox1.FindString(newT)
If i = -1 Then
e.Handled = True
End If
End Sub
回答by Mayank Raichura
While agree with previous answers that in order to restrict the users from entering invalid data, it is wise to select DropDownStyle = ComboBoxStyle.DropDownList
. But just in case you want what you want, you can use OnValidating
event of the control to check for valid values against the list items. Or better yet, inherit the control and use it through out the project. Here is the code that I use.
虽然同意之前的回答,为了限制用户输入无效数据,选择 是明智的DropDownStyle = ComboBoxStyle.DropDownList
。但万一您想要什么,您可以使用OnValidating
控件的事件来检查列表项的有效值。或者更好的是,继承控制权并在整个项目中使用它。这是我使用的代码。
Namespace Relax.Controls
Public Class RelaxCombobox
Inherits ComboBox
Public Property RestrictContentToListItems As Boolean = True
Public Sub New()
With Me
.AutoCompleteSource = Windows.Forms.AutoCompleteSource.ListItems
.AutoCompleteMode = Windows.Forms.AutoCompleteMode.SuggestAppend
End With
End Sub
Protected Overrides Sub OnValidating(e As System.ComponentModel.CancelEventArgs)
If RestrictContentToListItems AndAlso Me.Items.Count > 0 Then
Dim index As Integer = Me.FindString(Me.Text)
If index > -1 Then
Me.SelectedIndex = index
Else
e.Cancel = True
Me.Text = ""
Beep()
End If
End If
MyBase.OnValidating(e)
End Sub
End Class
End Namespace
Please note that not allowing the user to leave the control is a bad UI design.
请注意,不允许用户离开控件是一种糟糕的 UI 设计。
回答by Haroon Rasheed
try the following:
尝试以下操作:
Private Sub ComboBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.Leave
If (ComboBox1.SelectedIndex = -1) Then
ComboBox1.Focus()
End If
End Sub