locked
Combobox - prevent user input RRS feed

  • Question

  • Can anyone tell me if there is a property of a combobox that prevents the user from manually entering text?

    I only want the user to be able to select options from the the dropdown list.

     

    Thanks!

    Monday, April 3, 2006 4:59 PM

Answers

  • Set your ComboBox's DropDownStyle property to DropDownList.



    comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;

     


    Regards,

    -chris
    Tuesday, April 4, 2006 2:18 AM

All replies

  • Set your ComboBox's DropDownStyle property to DropDownList.



    comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;

     


    Regards,

    -chris
    Tuesday, April 4, 2006 2:18 AM
  • If anyone is like me and is not a fan of the style, then here is another option.  Create a custom control and override the textchange event.  This will keep the style of the combobox the same but it will only all the user to select an item.  You could also add a default index property if the combobox needs to have at least one item selected at all times.  Here is the code:

    Public Class ClickOnlyComboBox
        Inherits System.Windows.Forms.ComboBox
        Private mySelectedText As String
    
        Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
            MyBase.OnTextChanged(e)
            If Me.Items.Contains(Me.Text) Then
                mySelectedText = Me.Text
            Else
                Me.Text = mySelectedText
            End If
            Me.Select(0, Me.Text.Length)
        End Sub
    End Class

    ~Cheers!!
    • Proposed as answer by C Rogala Saturday, December 8, 2012 7:56 PM
    Saturday, December 8, 2012 7:56 PM