仅从组合框中选择列表项

时间:2013-02-20 13:36:10

标签: vb.net

我在visual studio 2010中有一个winforms应用程序。

在表单上,​​我有一个数据绑定组合框,为此我设置了autocompletesource = listitems和 autocompletemode = suggestappend。

现在为了使这项功能有效,我设置了dropdownstyle = dropdown,以便用户可以输入文字

但我希望用户只能从其下拉菜单中选择一个项目。

如果用户输入列表项以外的项目,并且离开组合框,则用户不应该离开组合框。

简而言之,我希望用户只能从可用的列表项中选择项目,而不是他输入的任何内容。

plz help

6 个答案:

答案 0 :(得分:4)

如果设置DropDownStyle = DropDownListAutoCompleteMode = Append,用户仍然可以输入值来选择他们想要的项目,但它们将仅限于列表中的项目。< / p>

AutoCompleteMode = Append时,它会检查后续字符,方法是将它们附加到正在搜索的值,只要您快速键入它们即可。如果你在击键之间等待太久,那么它将再次返回到第一个字母搜索。

考虑一下:你真的需要它们才能输入一个无效的值,这样你才能提醒它们它是无效的吗?因为如果没有,那就更混乱了。通过给予他们输入任何价值的机会,这意味着他们可以这样做。

答案 1 :(得分:3)

将属性'DropDownStyle'设置为'DropdownList',这将阻止用户输入组合。

希望这有帮助。

答案 2 :(得分:1)

我寻找一些解决方案,但没有使用限制DropDownList(键入是限时用户必须快速)。

以前的代码对我来说似乎不错,但在键入我们需要的内容时不会调用。 ComboBox我切换到AutoCompleteMode = SuggestAppendAutoCompleteSource = ListItemsDoprDownStyle = DropDown。这允许用户直接键入框并且没有时间限制。

这是我的代码,我希望对某人有所帮助:

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

答案 3 :(得分:0)

虽然同意之前的答案,但为了限制用户输入无效数据,最好选择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

请注意,不允许用户离开控件是一种糟糕的UI设计。

答案 4 :(得分:0)

尝试以下方法:

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

答案 5 :(得分:0)

只需将以下代码段添加到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
相关问题