vb.net - 组合框始终选择第一个值 [英] vb.net - combo box always select the first value
问题描述
晚安,任何人都可以帮我解决这个问题。我有两个组合框为月(01-12)月初始和monthend。现在,每次我选择十月(10),它显示的值是01.对不起。我是vb中的新人。
Good day, Can anyone help me with this problem. I have two a combo box for months(01-12) for monthstart and monthend. Now, everytime I select October(10), the value it show is 01. Sorry. I am new in vb.
有没有其他方法?任何建议?
Is there any alternative way to do this? Any suggestions?
谢谢。
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的值为January
The value of monthkeystart is 01
The value of monthvaluestart is January
推荐答案
这是因为您在读取值之前调用 ValueComboxformonth
方法。该方法重置组合框的数据源,默认为第一个值。
This is due to you calling the ValueComboxformonth
method before reading the values. That method resets the datasource for the combo-boxes, and it defaults to the first value.
尝试将该调用移动到表单的构造函数( New
方法)。
Try moving that call to the constructor (New
method) of the form.
Public Sub New()
...
ValueComboxformonth()
End Sub
这篇关于vb.net - 组合框始终选择第一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!