ComboBox SelectedText, why is it not switching to the SelectedText item?

10,358

Solution 1

I think you're misunderstanding what the SelectedText property is, refer to the MSDN documentation.

The SelectedText property is not the item from the list of items, it's the portion of an editable combobox that is selected, as if you were doing a copy/paste type of selection.

Your SetComboBoxToTextIndex method is the proper way to find and select an item in the list. Aternatively, if your ComboBoxItem properly implements Equals, you can find the appropriate instance and set the SelectedItem property.

Solution 2

This code will do what you want easily. ;)

myList.SelectedIndex = myList.FindString(myText);

Solution 3

ComboBox.SelectedText is equivalent to TextBox.SelectedText, i.e. it specifies the text that is selected inside the textbox of a combobox. It doesn't change the SelectedItem, because it has a completely different semantic.

Share:
10,358
Stefan Steiger
Author by

Stefan Steiger

I'm an avid HTTP-header-reader, github-user and a few more minor things like BusinessIntelligence & Web Software Developer Technologies I work with: Microsoft Reporting- & Analysis Service (2005-2016), ASP.NET, ASP.NET MVC, .NET Core, ADO.NET, JSON, XML, SOAP, Thrift ActiveDirectory, OAuth, MS Federated Login XHTML5, JavaScript (jQuery must die), ReverseAJAX/WebSockets, WebGL, CSS3 C#, .NET/mono, plain old C, and occasional C++ or Java and a little Bash-Scripts, Python and PHP5 I have a rather broad experience with the following relational SQL databases T-SQL PL/PGsql including CLR / extended stored procedures/functions Occasionally, I also work with MySQL/MariaDB Firebird/Interbase Oracle 10g+ SqLite Access I develop Enterprise Web-Applications (.NET 2.0 & 4.5) and interface to systems like LDAP/AD (ActiveDirectory) WebServices (including WCF, SOAP and Thrift) MS Federated Login OAuth DropBox XML & JSON data-stores DWG/SVG imaging for architecture In my spare-time, I'm a Linux-Server-Enthusiast (I have my own Web & DNS server) and reverse-engineer with interest in IDS Systems (IntrusionDetection), WireShark, IDA Pro Advanced, GDB, libPCAP. - Studied Theoretical Physics at the Swiss Federal Institute of Technology (ETHZ).

Updated on June 27, 2022

Comments

  • Stefan Steiger
    Stefan Steiger almost 2 years

    Question:

    My combobox (Me.cbHomeDrive) doesn't get properly initialized if I use

    Me.cbHomeDrive.SelectedText = "E:"
    

    On Form_Load:

    For i As Integer = AscW("C"c) To AscW("Z"c) Step 1
          Me.cbHomeDrive.Items.Add(New ComboBoxItem(ChrW(i) + ":"))
    Next
    
    Me.cbHomeDrive.SelectedIndex = 26 - 3
    Me.cbHomeDrive.Enabled = False
    

    With class ComboBoxItem being:

    Public Class ComboBoxItem
        Public Text As String
        Public ID As String
    
        Public Sub New(ByVal strText As String)
            Text = strText
            ID = strText
        End Sub
    
        Public Sub New(ByVal strText As String, ByVal strID As String)
            Text = strText
            ID = strID
        End Sub
    
    
        Public Overrides Function ToString() As String
            Return Text
        End Function
    End Class
    

    Now If I do

    Me.cbHomeDrive.SelectedText = "E:"
    

    right after

    Me.cbHomeDrive.Enabled = False
    

    Then nothing happens, and the combobox shows as Z:.

    If instead of

    Me.cbHomeDrive.SelectedText = "E:"
    

    I use

    SetComboBoxToTextIndex(Me.cbHomeDrive, "E:")
    

    with

    ' WTF '
    ' http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedtext.aspx '
    Sub SetComboBoxToTextIndex(ByVal cbThisComboBox As ComboBox, ByVal strItemText As String)
    
        For i As Integer = 0 To cbThisComboBox.Items.Count - 1 Step 1
            If StringComparer.OrdinalIgnoreCase.Equals(cbThisComboBox.Items(i).ToString(), strItemText) Then
                cbThisComboBox.SelectedIndex = i
                Exit For
            End If
        Next
    
    End Sub
    

    Then it sets the correct selected item (E:).

    Why does it not work with Me.cbHomeDrive.SelectedText = "E:"?