vb.net - 组合框总是选择第一个值

时间:2015-12-10 16:52:59

标签: vb.net winforms combobox

美好的一天,任何人都可以帮我解决这个问题。我有两个月份(01-12)的组合框用于monthstart和monthend。现在,每当我选择十月(10)时,它显示的值是01.抱歉。我是vb的新手。

有没有其他方法可以做到这一点?有什么建议吗?

感谢。

 Private Sub ValueComboxformonth()
    Dim comboSource As New Dictionary(Of String, String)()
    comboSource.Add("01", "January")
    comboSource.Add("02", "February")
    comboSource.Add("03", "March")
    comboSource.Add("04", "April")
    comboSource.Add("05", "May")
    comboSource.Add("06", "June")
    comboSource.Add("07", "July")
    comboSource.Add("08", "August")
    comboSource.Add("09", "September")
    comboSource.Add("10", "October")
    comboSource.Add("11", "November")
    comboSource.Add("12", "December")

    cmbAppliedMonthStart.DataSource = New BindingSource(comboSource, Nothing)
    cmbAppliedMonthStart.DisplayMember = "Value"
    cmbAppliedMonthStart.ValueMember = "Key"

    cmbAppliedMonthEnd.DataSource = New BindingSource(comboSource, Nothing)
    cmbAppliedMonthEnd.DisplayMember = "Value"
    cmbAppliedMonthEnd.ValueMember = "Key"

End Sub


 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    ValueComboxformonth()
    Dim monthkeystart As String = DirectCast(cmbAppliedMonthStart.SelectedItem, KeyValuePair(Of String, String)).Key
    Dim monthvaluestart As String = DirectCast(cmbAppliedMonthStart.SelectedItem, KeyValuePair(Of String, String)).Value

    Dim monthkeyend As String = DirectCast(cmbAppliedMonthEnd.SelectedItem, KeyValuePair(Of String, String)).Key
    Dim monthvalueend As String = DirectCast(cmbAppliedMonthEnd.SelectedItem, KeyValuePair(Of String, String)).Value
End Sub

monthkeystart的值为01
monthvaluestart的值是1月

1 个答案:

答案 0 :(得分:1)

这是因为您在读取值之前调用了ValueComboxformonth方法。该方法重置组合框的数据源,默认为第一个值。

尝试将该调用移动到表单的构造函数(New方法)。

Public Sub New()
    ...
    ValueComboxformonth()
End Sub