Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pass CommandButton array to Class's method

Status
Not open for further replies.

jamsek19

Programmer
Feb 4, 2002
35
SI
I have one form with four CommandButtons putted in an array (Cmd(0), Cmd(1), …)
I have also one class which operate with these buttons. I have a problem, how to pass reference of button array to one class's method.

Example:
Class clsBtn:
Private mBtn(4) As CommandButton
Friend Sub AddButtons(ByRef a_btn() As CommandButton)
Set mBtn = a_btn
End Sub

Form form1:
Private cBtn as claBtn
Public Sub Form_init()
Set cBtn = new clsBtn
cBtn.AddButtons form1.Cmd
End Sub

This way got error "Array or user defined type expected" at line with cBtn.AddButtons form1.btn

If I reorded above methods and properties this way
Class clsBtn:
Private mBtn1 As CommandButton
Private mBtn2 As CommandButton
Private mBtn3 As CommandButton
Private mBtn4 As CommandButton
Friend Sub AddButton1(ByRef a_btn As CommandButton)
Set mBtn1 = a_btn
End Sub
Friend Sub AddButton2(ByRef a_btn As CommandButton)
Set mBtn2 = a_btn
End Sub


Form form1:
Private cBtn as claBtn
Public Sub Form_init()
Set cBtn = new clsBtn
cBtn.AddButton1 form1.Cmd(0)
cBtn.AddButton2 form1.Cmd(1)

End Sub
I got RunTimeError 13: Type Mismatch in a line cBtn.AddButton1 form1.Cmd(0).

Can anyone help me? Thanks in advance.


 
Try something like this:

'form
Private cBtn As clsBtn
Private Sub Form_Load()
Set cBtn = New clsBtn
cBtn.AddButton1 Form1.Cmd()
End Sub

'class

Private mBtn(4) As CommandButton

Friend Sub AddButton1(ByRef a_btn As Object)
Dim i As Integer
Dim cmdButton As CommandButton
For Each cmdButton In a_btn
Set mBtn(i) = cmdButton
i = i + 1
Next
mBtn(0).Caption = "Button 1"
mBtn(1).Caption = "Button 2"
mBtn(2).Caption = "Button 3"
mBtn(3).Caption = "Button 4"
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top