【发布时间】:2011-07-23 00:03:09
【问题描述】:
问题:
如果我使用,我的组合框 (Me.cbHomeDrive) 无法正确初始化
Me.cbHomeDrive.SelectedText = "E:"
在 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
ComboBoxItem 类为:
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
如果我这样做了
Me.cbHomeDrive.SelectedText = "E:"
紧接着
Me.cbHomeDrive.Enabled = False
然后什么都没有发生,组合框显示为 Z:。
如果不是
Me.cbHomeDrive.SelectedText = "E:"
我用
SetComboBoxToTextIndex(Me.cbHomeDrive, "E:")
与
' 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
然后它设置正确的选定项 (E:)。
为什么它不适用于 Me.cbHomeDrive.SelectedText = "E:"?
【问题讨论】:
标签: c# vb.net winforms combobox