我正在尝试编写一个通用例程来填充表单加载时的组合框。我从MySql获取数据并填充组合框,但它不适用于组框内的任何组合框。尝试传递formname,groupbox名称,comboname byVal和byRef,尝试传入作为对象,字符串,控件,尝试各种ctypes和trycast,似乎无法找到神奇的语法。我有一个包含2个组合框的测试表单,一个在外部,一个在groupbox和此代码中。
Public Function TESTloadComboBoxOrHelp(ByRef formName As Form, ByVal boxName As Object, ByVal callingName As String) ', ByVal groupBx)
'loadComboBoxOrHelp(me,"comboboxname",me.name)
'tried passing byVal and byRef, no difference
'frmCmbTest has 2 combo boxes, one in a groupbox and one not in groupbox, the one NOT in works
'
If formName.Name.ToString = "frmCmbTest" Then
CType(formName.Controls(boxName), ComboBox).Items.Add("ABC") ' 1: this WORKS because combobox1 is NOT in a groupbox BUT does not work for combobox2 in groupbox
frmCmbTest.ComboBox2.Items.Add("ZYZ") ' 2: NOT Generic, this WORKS even though combobox2 is in a groupbox so why does line 1: above NOT work for a groupbox ?
'formName.boxName.Items.Add("ZYZ") ' 2.1: this does NOT work even though its the equivalent syntax (didn't expect it to)
'formName.groupBx.boxName.Items.Add("ZYZ") ' 2.2: NOPE, Tried passing in the name of the groupbox
'frmCmbTest.CType(formName.controls("Groupbox1"), GroupBox),CType(formName.Controls.boxName.Name.ToString), ComboBox).Items.Add("DEF") ' 3: so of course this should not work, tried to replicate line above (2:) could not figure out syntax that would work.
'frmCmbTest.GroupBox1.ComboBox2.Items.Add("ZYZ") ' 4: does not work, as is expected not to.
'TryCast(formName."GroupBox1".Controls(boxName.Name), ComboBox).Items.Add("MNO") '5: I am guessing I need to add the groupbox name somewhere
TryCast(formName.Controls(boxName), ComboBox).Items.Add("MNO") ' 6: out of ideas, works for combobox1 not in groupbox, does not work for combobox2 in groupbox1
End If
Return 1
End Function
目前我有5个函数,一个用于填充不在各种形式的groupbox中的组合框的通用函数和4个非通用的函数,它让我疯狂。
答案 0 :(得分:1)
您正在处理2个不同的ControlCollections
,因为看起来您使用的是Windows窗体,Control Colllection有一个Find
方法,用于检查项目是否在父集合及其子集中。它将返回所有匹配项的数组,因此请确保您的设备名称是唯一的,而不是彼此的子集。 即。 boxName和boxName1搜索boxName,为您提供包含2个项目的结果
Dim c As Control() = Controls.Find("boxName", True) 'Search for boxName in parent and all child controls
If c.Count > 0 Then 'Check to see if we got a match
CType(c(0), ComboBox).Items.Add("ABC")
End If
答案 1 :(得分:1)
根据我对马克的评论,他的回答是有效的! 这就是我最终的结果(供将来参考)
Public Function TESTloadComboBoxOrHelp(ByRef formName As Form, ByVal boxName As Object, ByVal callingName As String) ', ByVal groupBx)
'loadComboBoxOrHelp(me,"comboboxname",me.name)
'
Dim c As Control() = formName.Controls.Find(boxName, True) 'Search for boxName in parent and all child controls
If c.Count > 0 Then 'Check to see if we got a match
Try
conn.Open()
myCommand.Connection = conn
Dim query = "SELECT name, lookupValue " _
& "FROM lookups " _
& "WHERE name = '" & formName.Name.ToString & "." & boxName.ToString & "'"
myCommand.CommandText = query
If debug Then evl.WriteToSimpleLog(query, callingName)
myReader = myCommand.ExecuteReader()
While myReader.Read
CType(c(0), ComboBox).Items.Add(myReader.GetString("lookupValue").ToString)
End While
Catch ex As Exception
MessageBox.Show("Error while retrieving records on table Lookups..." & ex.Message, "Lookups Table")
Finally
If conn.State = ConnectionState.Open Then conn.Close()
End Try
End If
我也会尝试建议的数据绑定,我只是没有想到,但这终于有效了,我讨厌我一直在使用的解决方法