这是填充组合框的正确方法吗?
表单上的组合框命名为cmbType1
、cmbType2
、cmbType3
。。。等等
我使用以下类型的逻辑来初始化组合框:
Private Sub fillCombo(count As Integer)
Dim cmbControl As Object
For i = 1 To count
Set cmbControl = Me.Controls.item("cmbType" + CStr(i))
cmbConnectorTypeControl.AddItem ("ABC")
Next i
End Sub
所以我只想交叉验证一下,这是正确的编码风格吗?
欢迎提出任何建议。。。
Alex是正确的。如果您的组合框已经在表单上,并且您的通用fillCombo方法有效,则尝试更改该方法以ComboBox作为参数。它将节省查找的时间,并简化代码。我假设您并没有真正将"ABC"添加到计数之前的所有组合框中,但即使您添加了,我也宁愿在循环中调用此方法。
Private Sub fillCombo(ByVal vCombobox As ComboBox)
vCombobox.AddItem ("ABC")
End Sub
您可以跳过指定初始计数的需要;
fillCombo "cmbType"
...
function fillCombo(name)
dim ctrl as Control
for each Control in Me.Controls
if typeof Control is ComboBox then
if left$(Control.name, len(name)) = name then
Control.additem "ABC"
end if
end if
next
end function