VB中的函數(shù)可以使用數(shù)組形參,但是卻不能傳遞控件數(shù)組,原因是VB中的控件數(shù)組和數(shù)組本身的構造方式不太一樣,雖然同是在內存中順序排列,但是調用方法卻有小小區(qū)別,控件數(shù)組的使用更象是一個集合。數(shù)組的使用
僅僅只能通過Lboun和Ubound函數(shù)來獲取數(shù)組上下標,而控件數(shù)組則可使用control.Lbound,control.ubound屬性來獲取上下標。數(shù)組中訪問其元素只能使用Arr(Index)的方式,但控件數(shù)組則還可以通過control.item(index)來訪問。由于這點小小的不同,造成了控件數(shù)組不能當作函數(shù)參數(shù)傳遞的問題。 現(xiàn)在我們通過2種方式來解決??!2種方式實現(xiàn)各不相同,所能應用的范圍也不一樣。 第一種使用對象數(shù)組的方法:(例子說明) 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 第二種方式,傳遞控件數(shù)組中一個元素。(這種方式有點取巧) 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》