Hemal Rathod
Hemal Rathod

Reputation: 93

Only select list items from combobox

I have a winforms application in visual studio 2010.

On a form, I have a databound combobox, for which i have set autocompletesource=listitems and autocompletemode=suggestappend.

Now For this functionality to work, I have set dropdownstyle=dropdown, so that user can type a text

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

Upvotes: 3

Views: 33623

Answers (6)

Laxmikant Bhumkar
Laxmikant Bhumkar

Reputation: 528

Just add the following snippet to KeyPress event of ComboBox. Remember to replace Combobox name with yours one.

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

Upvotes: 0

FreezeGhost
FreezeGhost

Reputation: 11

I look for some solution but without using limiting DropDownList (typing is time limited users must be quick).

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.

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

Upvotes: 1

Haroon Rasheed
Haroon Rasheed

Reputation: 1

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

Upvotes: 0

Mayank Raichura
Mayank Raichura

Reputation: 381

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.

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.

Upvotes: 0

Steven Doggart
Steven Doggart

Reputation: 43743

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.

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.

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.

Upvotes: 6

Cubsoft
Cubsoft

Reputation: 1157

Set the property 'DropDownStyle' to 'DropdownList' and this will stop the user from typing into the combo.

Hope this helps.

Upvotes: 3

Related Questions