对于每个循环不循环 [英] For each loop not looping
问题描述
我正在尝试在运行时填充一些组合框。我在一个单独的模块中有这个代码,有其他形式调用这个子,所有工作正常。
当我运行代码的msg框的计数组合框返回6,但循环只运行一次??
感谢任何帮助。
< pre lang =vb> 公共 Sub PopulateCombos(myForm 正如表格)
Dim mySQL As 字符串 =
Dim myDispMember 作为 字符串
Dim myValue 作为 字符串
昏暗跨度> x 作为 整数 = 0
' 调用SQL.Exceute查询 - 查询进入括号
' 遍历所有组合框
对于 每个 myCombo As ComboBox < span class =code-keyword>在 myForm.Controls.OfType( of ComboBox).ToArray
' 重置字符串
mySQL =
myDispMember =
myValue =
MsgBox(myForm.Name)
MsgBox(myCombo。名称)
MsgBox(myForm.Controls.OfType( Of ComboBox).Count())
x = x + 1
MsgBox(x)
选择 案例 myCombo.Name.ToString
案例 cboClient ' 填充客户列表
mySQL = SELECT id,strSname,strFName FROM tblContacts
myDispMember = < span class =code-string> strSName
myValue =
Case cboStatus ' < span class =code-comment>填充cboStatus
mySQL = SELECT id,strProjStatus FROM tblProjectStatus
myDispMember = strProjStatus
myValue = id
结束 选择
SQL.ExecQuery(mySQL)
< span class =code-keyword>如果 SQL.SQLDS.Tables.Count> 0 然后
带 myCombo
.DisplayMember = myDispMember
.ValueMember = myValue
.DataSource = SQL.SQLDS.Tables( 0 )
.AutoCompleteMode = AutoCompleteMode.SuggestAppend
.AutoCompleteSource = AutoCompleteSource.ListItems
End 使用
结束 如果
下一步 i
结束 Sub 除了Next i (为什么?!):你可能没有这种类型的任何控制 表格。这些控件很少直接放在表单上,更典型的是,它们作为一些嵌套面板和其他容器控件(例如标签页)的子项放置。你需要在一个单独的函数中抽象出循环,并在其实现中 递归地 调用它。
< DD> -SA
I am trying to populate some combo boxes at run time. I have this code in a separate module, there are other forms that call this sub and all work fine.
When I run the code the msg box for the count of combo boxes returns 6 but the loop only runs once??
Any help gratefully received.
Public Sub PopulateCombos(myForm As Form)
Dim mySQL As String = ""
Dim myDispMember As String
Dim myValue As String
Dim x As Integer = 0
'Call the SQL.Exceute query - query goes in the the parentheses
'loop through all the combo boxes
For Each myCombo As ComboBox In myForm.Controls.OfType(Of ComboBox).ToArray
'reset the strings
mySQL = ""
myDispMember = ""
myValue = ""
MsgBox(myForm.Name)
MsgBox(myCombo.Name)
MsgBox(myForm.Controls.OfType(Of ComboBox).Count())
x = x + 1
MsgBox(x)
Select Case myCombo.Name.ToString
Case "cboClient" 'populate client list
mySQL = "SELECT id, strSname, strFName FROM tblContacts"
myDispMember = "strSName"
myValue = "id"
Case "cboStatus" 'Populate cboStatus
mySQL = "SELECT id, strProjStatus FROM tblProjectStatus"
myDispMember = "strProjStatus"
myValue = "id"
End Select
SQL.ExecQuery(mySQL)
If SQL.SQLDS.Tables.Count > 0 Then
With myCombo
.DisplayMember = myDispMember
.ValueMember = myValue
.DataSource = SQL.SQLDS.Tables(0)
.AutoCompleteMode = AutoCompleteMode.SuggestAppend
.AutoCompleteSource = AutoCompleteSource.ListItems
End With
End If
Next i
End Sub
In addition to "Next i" (why?!): you may not have any controls of this type of the form. Such controls are rarely put on a form directly, more typically, they are put as children of some nested panels and other container controls (tab pages, for example). You need to abstract out the loop in a separate function and recursively call it in its implementation.
—SA
这篇关于对于每个循环不循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!