Can't reset ComboBox default text.

Mugsy's RapSheet 196 Reputation points
2021-11-30T01:15:52.7+00:00

I have a ComboBox on my form. Default text is "Select Type".

One of my options is not yet supported. So if the user selects it, I display a MsgBox and then attempt to reset it. But for some reason, no matter what I do, I can't reset it to display the default text again. I've tried all variations of the following:

Properties:
ComboBox.Text = "Select Type"
ComboBox.Items are "Option #1" & "Option #2".

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedIndex = 1 Then
MsgBox("Contents: '" & ComboBox1.Text & "'")
ComboBox1.SelectedIndex = -1
ComboBox1.SelectedItem = -1
ComboBox1.ResetText()
ComboBox1.Text = "Select Type"
End If
End Sub

Setting "SelectedIndex = -1" simply erases the text.
The other three setting ("SelectedItem", "ResetText()" and "Text = "Reset"") all simply re-highlights the selected item.

No combination of those options work. Nothing I try resets the default text to appear.

Is this a bug in VS2019? Any work-around? TIA

Developer technologies VB
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,521 Reputation points
    2021-11-30T16:24:13.023+00:00

    You can use a Banner

    For example :

    153793-combobox-banner.gif

        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged  
            If ComboBox1.SelectedIndex = 1 Then  
                MsgBox("Contents: '" & ComboBox1.Text & "'")  
                ComboBox1.SelectedIndex = -1  
                Dim pMessage As IntPtr = Marshal.StringToHGlobalUni("Select Type")  
                If ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList Then  
                    SendMessage(ComboBox1.Handle, CB_SETCUEBANNER, 0, pMessage)  
                Else  
                    Dim hEdit As IntPtr = GetWindow(ComboBox1.Handle, GW_CHILD)  
                    SendMessage(hEdit, EM_SETCUEBANNER, 0, pMessage)  
                    ActiveControl = Nothing  
                End If  
            End If  
        End Sub  
    

    with :

    Public Const ECM_FIRST = &H1500  
    Public Const EM_SETCUEBANNER = (ECM_FIRST + 1)  
    Public Const CB_SETCUEBANNER As Integer = &H1703  
      
    <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>  
    Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer  
    End Function  
      
    <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>  
    Public Shared Function GetWindow(hWnd As IntPtr, uCmd As UInteger) As IntPtr  
    End Function  
    
    Public Const GW_HWNDFIRST = 0  
    Public Const GW_HWNDLAST = 1  
    Public Const GW_HWNDNEXT = 2  
    Public Const GW_HWNDPREV = 3  
    Public Const GW_OWNER = 4  
    Public Const GW_CHILD = 5  
    

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-11-30T03:45:52.587+00:00

    Here is a path to consider, its not exactly what you want but shows an alternate path to take.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
    
            ComboBox1.DataSource = New List(Of Item) From {
                New Item() With {.Text = "Select Type", .Identifier = -1},
                New Item() With {.Text = "Option 1", .Identifier = 1},
                New Item() With {.Text = "Option 2", .Identifier = 2},
                New Item() With {.Text = "Option 3", .Identifier = 3}
            }
    
            ComboBox1.SelectedIndex = -1
    
            AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
    
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
            Dim item = CType(ComboBox1.SelectedItem, Item)
            If item.Identifier = -1 OrElse item.Identifier = 3 Then
                RemoveHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
                ComboBox1.SelectedIndex = -1
                MessageBox.Show("Please make a selection")
                AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
            Else
                MessageBox.Show($"{item.Identifier} - {item.Text}")
            End If
        End Sub
    End Class
    Public Class Item
        Public Property Text() As String
        Public Property Identifier() As Integer
    
        Public Overrides Function ToString() As String
            Return Text
        End Function
    End Class
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.