vb.net中的ComboBox函数 [英] ComboBox function in vb.net
本文介绍了vb.net中的ComboBox函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
伙计们,
如何在vb.net中为组合框编写一个函数,该函数将帮助填充州的多个组合框,而另一个函数将根据从状态组合框中选择的每个州为城市填充另一个组合框.我具有此功能来处理组合框的状态.
Good day guys,
how to do I write a function in vb.net for combobox that will help populate multiple combobox for state and another function that will populate another comboboxes for cities based on each state selected from state combobox. I have this function to handle combobox for state.
Private Sub PopulateStateCombo(ByVal cbox As ComboBox)
cbox.Items.Add("Newyork")
cbox.Items.Add("California")
cbox.Items.Add("Texas")
cbox.Items.Add("Arizona")
End Sub
我像PopulateStateCombo(ComBoxState)这样调用.
谢谢您的期待.
which I callup like PopulateStateCombo(ComBoxState).
Thank you in anticipation.
推荐答案
下面是有关如何实现目标的示例代码.
要使用代码创建一个新的WindowsApplication,然后将两个组合框(分别命名为cmbState和cmbCity)拖到form(form1)上,然后将代码粘贴到form1
(注意:不要因代码量而感到困惑.
实际的实现在cmbState_SelectionChangeCommitted()和form1_load()事件中.
generateData()只是生成要馈送到组合框的数据)
below is a sample code as to how you can achieve your goal.
To use the code create a new windowsApplication, then drag two combobox(name it cmbState and cmbCity) to the form(form1) then paste the code to form1
(Note: don''t get confuse by the amount of code.
The actual implementation is in cmbState_SelectionChangeCommitted() and form1_load() events.
The generateData() simply generates data to be fed to the combobox)
Dim stateDT As DataTable ''in memory table for storing state data (datatable can easily be filled with data from your Database)
Dim cityDt As DataTable '' in memory table for storing city data (datatable can easily be filled with data from your Database)
''bindingSource has a filter property which we will make use of to filter the cityDT as per the state chosen
Dim cityBS As BindingSource
'''''' <summary>
'''''' initialize and fill stateDT and cityDt with state and city data respectively
'''''' note: to be able to chose city from the state chosen we need a connection between these two data groups and we
'''''' have ''stateSno'' in both citydt and statedt as the connecting column
'''''' </summary>
'''''' <remarks></remarks>
Sub generateData()
stateDT = New DataTable
stateDT.Columns.Add("stateSno", GetType(Integer))
stateDT.Columns.Add("stateName", GetType(String))
stateDT.Rows.Add(0, "Newyork")
stateDT.Rows.Add(1, "California")
cityDt = New DataTable
cityDt.Columns.Add("stateSno", GetType(Integer)) ''this is a related column to stateDt->stateSNo ,so we just need to use filter
cityDt.Columns.Add("cityName", GetType(String))
''cities for Newyork
cityDt.Rows.Add(0, "NY_city1")
cityDt.Rows.Add(0, "NY_city2")
''cities for California
cityDt.Rows.Add(1, "Cal_city1")
cityDt.Rows.Add(1, "Cal_city2")
cityDt.Rows.Add(1, "Cal_city3")
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
generateData()
''here we feed state data to the State combobox using datasource property
cmbState.DataSource = stateDT ''feed state data directly to combobox
cmbState.DisplayMember = "stateName" ''the member displayed in the combobox text
cmbState.ValueMember = "stateSno" ''the value returned when we call combobox.selectedvalue
cityBS = New BindingSource ''I suggest you get use to using binding source if you need to filter or sort data
cityBS.DataSource = cityDt ''feed city data to cityBS
cmbCity.DataSource = cityBS '' then feed cityBS to the city combobox so that we can use cityBS''s filter property
cmbCity.DisplayMember = "cityName" ''the member displayed in the combobox text
''explicitly set state to index 0 and call cmbState_SelectionChangeCommitted() to filter city as the per the state selected
cmbState.SelectedIndex = 0
cmbState_SelectionChangeCommitted(sender, e)
End Sub
Private Sub cmbState_SelectionChangeCommitted(sender As Object, e As System.EventArgs) Handles cmbState.SelectionChangeCommitted
If cmbState.SelectedIndex < 0 Then
cityBS.Filter = "stateSNo = -1" ''if no item is selected then we set it to a state no.(in this case -1) which we know will never be assigned to a state
Else
cityBS.Filter = "stateSNo = " & cmbState.SelectedValue ''simply filter cityBS with the cmbState selectedValue to get the desired result
End If
End Sub
这篇关于vb.net中的ComboBox函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文