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