ComboBox.SelectedValue仅在Windows XP中重置

时间:2014-03-12 19:12:31

标签: vb.net winforms data-binding combobox portability

我在WinForms ComboBox课程中遇到了一些问题。我想要实现的是在找到匹配项后以编程方式从SelectedValue处理程序设置ComboBox.TextChanged。这在Windows 7上运行良好,但在Windows XP中SelectedValue将被设置并且SelectedValueChanged将被引发,但一旦到达Validating SelectedValue,则返回Nothing 。似乎在XP中更改SelectedValue的唯一方法是从下拉列表中选择一些内容。

这是一个只有Form和多行ComboBox的玩具TextBox

XP:输入ComboBox为1Y,然后按Tab键。输出:

SelectedValueChanged: SelectedValue: 1X
Validating: SelectedValue: Nothing
Value is: 
Validated: SelectedValue: Nothing

Win7:输入ComboBox是1Y,然后按Tab键。输出:

SelectedValueChanged: SelectedValue: 1X
Validating: SelectedValue: 1X
Value is: 1X
Validated: SelectedValue: 1X

代码:

Public Class ComboBoxXPForm
    Private WithEvents mData As New DataHolder
    Private mBindingSrc As BindingSource


    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        mBindingSrc = New BindingSource() With {.DataSource = mData}

        Dim dt As New DataTable()
        dt.Columns.Add("value", GetType(String))
        dt.Columns.Add("displayValue", GetType(String))

        dt.Rows.Add("", "")
        For i = 1 To 10
            dt.Rows.Add(i & "X", i & "Y")
        Next

        cboBox.DataSource = dt
        cboBox.ValueMember = "value"
        cboBox.DisplayMember = "displayValue"

        cboBox.DataBindings.Add("SelectedValue", mBindingSrc, "StrVal", True)
    End Sub

    Private Sub cboBox_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboBox.SelectedValueChanged
        txtDebug.Text &= vbNewLine & "SelectedValueChanged: SelectedValue: " & _
            If(cboBox.SelectedValue Is Nothing, _
               "Nothing", cboBox.SelectedValue.ToString())
    End Sub

    Private Sub DataHolder_DebugInfo(ByVal sender As Object, ByVal e As DebugEventArgs) Handles mData.DebugInfo
        txtDebug.Text &= vbNewLine & e.DebugInfo
    End Sub

    Private Sub cboBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboBox.TextChanged
        'For Each row In cboBox.Items
        '    If cboBox.Text = row("displayValue") AndAlso row("value") <> cboBox.SelectedValue Then
        '        'cboBox.SelectedValue = row("value")
        '        cboBox.SelectedItem = row
        '    End If
        'Next
        For i = 0 To cboBox.Items.Count - 1
            Dim item = cboBox.Items(i)
            If cboBox.Text = item("displayValue") Then
                cboBox.SelectedIndex = i
            End If
        Next
    End Sub

    Private Sub cboBox_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboBox.Validated
        txtDebug.Text &= vbNewLine & "Validated: SelectedValue: " & _
            If(cboBox.SelectedValue Is Nothing, _
               "Nothing", cboBox.SelectedValue.ToString())
    End Sub

    Private Sub cboBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles cboBox.Validating
        txtDebug.Text &= vbNewLine & "Validating: SelectedValue: " & _
            If(cboBox.SelectedValue Is Nothing, _
               "Nothing", cboBox.SelectedValue.ToString())
    End Sub
End Class

Public Class DebugEventArgs
    Inherits EventArgs

    Private mDebugInfo As String

    Public Sub New(ByVal DebugString As String)
        MyBase.New()
        DebugInfo = DebugString
    End Sub

    Public Property DebugInfo() As String
        Get
            Return mDebugInfo
        End Get
        Set(ByVal value As String)
            mDebugInfo = value
        End Set
    End Property
End Class

Public Class DataHolder
    Public Event DebugInfo(ByVal sender As Object, ByVal e As DebugEventArgs)

    Private mStrVal As String
    Public Property StrVal() As String
        Get
            Return mStrVal
        End Get
        Set(ByVal value As String)
            mStrVal = value
            RaiseEvent DebugInfo(Me, New DebugEventArgs("Value is: " & value))
        End Set
    End Property
End Class

1 个答案:

答案 0 :(得分:1)

尝试将AutoCompleteMode变为SuggestAppend,并将AutoCompleteSource变为ListItems。然后你可以删除整个TextChanged代码块,它也应该在XP上正常工作。