开发者

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

开发者 https://www.devze.com 2023-02-19 14:04 出处:网络
Question: My combobox (Me.cbHomeDrive) doesn\'t get properly initialized if I use Me.cbHomeDrive.SelectedText = \"E:\"

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开发者_StackOverflow社区:).

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


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.


This code will do what you want easily. ;)

myList.SelectedIndex = myList.FindString(myText);


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.


If the item is of type object You can use SelectedItem

ComBaudRate.ValueMember = "Value";
ComBaudRate.DisplayMember = "Text";
string[] baudValues = {"1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200"};
ComBaudRate.Items.Clear();
for(var idx = 0; idx < baudValues.Length; idx++)
{
     ComBaudRate.Items.Add(new { Text = baudValues[idx], Value = baudValues[idx] });
}    
ComBaudRate.SelectedItem = new { Text = "19200", Value = "19200" };
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号