VB中的函数可以使用数组形参,但是却不能传递控件数组,原因是VB中的控件数组和数组本身的构造方式不太一样,虽然同是在内存中顺序排列,但是调用方法却有小小区别,控件数组的使用更象是一个集合。数组的使用
仅仅只能通过Lboun和Ubound函数来获取数组上下标,而控件数组则可使用control.Lbound,control.ubound属性来获取上下标。数组中访问其元素只能使用Arr(Index)的方式,但控件数组则还可以通过control.item(index)来访问。由于这点小小的不同,造成了控件数组不能当作函数参数传递的问题。 现在我们通过2种方式来解决!!2种方式实现各不相同,所能应用的范围也不一样。 第一种使用对象数组的方法:(例子说明) private sub SendControls() Dim Arr_Chk() as CheckBox Dim Int_I As Integer ReDim Arr_Chk(Chk_Tmp.Lbound To Chk_Tmp.Ubound) For Int_I =Chk_Tmp.Lbound to Chk_Tmp.Ubound Set Arr_Chk(Int_I)=Chk_Tmp.Item(Int_I) next Call TestControls(Arr_Chk) end sub private sub TestControls(ByRef TestControls() As CheckBox) Dim Int_I as Integer For Int_I=Lbound(TestControls) To Ubound(TestControls) debug.pring TestControls(Int_I).Name & " " & TestControls(Int_I).Value next End Sub 第二种方式,传递控件数组中一个元素。(这种方式有点取巧) Private Sub SendControls() call TestControls(Chk_Tmp.Item(Chk_Tmp.Lbound)) end sub Private Sub TestControls(byval TestControl as CheckBox) Dim TmpControl as Object For Each TmpControl In Controls If TmpControl.Name=TestControl.Name Then Debug.Print TmpControl.Name & " " & TmpControl.Value end if Next End Sub |
|
来自: chinablank > 《vb》