vb.net - 組合框總是選擇第一個值 (vb.net - combo box always select the first value)


問題描述

vb.net ‑ 組合框總是選擇第一個值 (vb.net ‑ combo box always select the first value)

美好的一天,誰能幫我解決這個問題。我有兩個用於月份(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:

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.

Try moving that call to the constructor (New method) of the form.

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

(by jLawSam Makin)

參考文件

  1. vb.net ‑ combo box always select the first value (CC BY‑SA 2.5/3.0/4.0)

#Combobox #winforms #vb.net






相關問題

tcl/tk 小部件組合框失去焦點 (tcl/tk widget combobox loses focus)

Flex Combobox 奇怪的問題 (Flex Combobox strange problem)

在組合框中對齊文本 (Align Text in Combobox)

如何同時綁定到 ComboBox 中的兩個不同的依賴屬性? (How do I bind to two different dependency properties in a ComboBox at the same time?)

在綁定到字典的組合框中設置所選項目 (Setting selected item in combobox bound to dictionary)

easyUI datagrid內部編輯組合框無法選擇默認值 (easyUI datagrid inner edit combobox cannot selected default value)

VBA:數據輸入組合框有效,但命令按鈕給出“無效的屬性值”錯誤 (VBA: Data entry into combobox works, but command button gives "Invalid property value" error)

vb.net - 組合框總是選擇第一個值 (vb.net - combo box always select the first value)

打開 ComboBox DropDown 時不要選擇文本輸入 (Do not select text input when ComboBox DropDown is opened)

如何使用相同的數據源設置多個組合框? (How can I set up multiple combo-boxes with the same data source?)

如何使用 QtreeView 在 QComboBox 中設置所選項目 (How to set selected item in QComboBox with QtreeView)

Tkinter 組合框 Python (Tkinter Combobox Python)







留言討論