2

I have a DropDown and a DropDownList on my form. I am aware that a DropDown can hold a placeholder text and a DropDownList cannot, however; I would like some code or a work around to allow either:

  • DropDown as read-only, therefore not allowing a user to type
  • But preferably a DropDownList with placeholder text (context menu option, or not)

Is this possible?

enter image description here

Thanks.

  • possible duplicate of [How to show text in combobox when no item selected?](http://stackoverflow.com/questions/8064133/how-to-show-text-in-combobox-when-no-item-selected) – Matt Wilko Jul 09 '15 at 12:41
  • You should already have a class derived from ComboBox to display the cue. Simply override OnKeyPress and set e.Handled = True. – Hans Passant Jul 09 '15 at 12:49
  • You could always add an extra item to the DropDownList and set it as the default selected option on page load to act as the placeholder. – Jon Jul 09 '15 at 13:23

1 Answers1

0

But preferably a DropDownList with placeholder text (context menu option, or not)

By definition, the text displayed in this control is always the text of the selected item. You can add a "fake" item to the list if you want (e.g. "Select Property Code"), but you will have to check that this item isn't selected later.

To display one of the items (fake or not), simply set the SelectedIndex to the appropriate value once the list is loaded.

DropDown as read-only, therefore not allowing a user to type

This will actively make sure that the text is either in the list or a default value, effectively making it read only. (Written for a ComboBox, but the behavior should be identical with a DropDown.)

Private Sub ComboBox1_TextChanged(sender As Object, e As EventArgs) Handles ComboBox1.TextChanged
    Static recursion As Boolean = False
    Dim defaultText As String = "My Default Text"
    If recursion Then
        recursion = False
    Else
        If ComboBox1.Items.Count > 0 Then
            For i As Integer = 0 To ComboBox1.Items.Count - 1
                If ComboBox1.Items(i).ToString = ComboBox1.Text Then
                    Exit Sub
                End If
            Next
        recursion = True
        ComboBox1.Text = defaultText
    End If
End Sub

Alternatively, here is a sub I call whenever a "strict" ComboBox looses focus to accomplish the same thing. The difference is that doing it this way allows you to keep the AutoComplete functionality:

Public Sub EnforceList(ByRef box As ComboBox)   'FORCES .TEXT TO BEST (OR FIRST) MATCH IN .ITEMS
    'If list contains item whose name begins another item's, the shorter must be listed first, e.g. "sew" must preceed "sewer"
    If box.Items.Count = 0 Then Exit Sub 'Can't enforce a list that doesn't exist
    Dim txt As String = box.Text
    Do
        For i As Integer = 0 To box.Items.Count - 1
            If box.Items(i).ToString Like txt & "*" Then
                box.Text = box.Items(i).ToString
                Exit Sub
            End If
        Next
        txt = Left(txt, Len(txt) - 1)
    Loop
End Sub
Josh
  • 1,088
  • 1
  • 7
  • 16