Class module to make controls

veebee123
08-09-2009, 10:35 PM
I have a program with plugins. The plugins have to be able to create controls on the host application. I am trying to make an API for my plugins to make it easier to create the controls.

I have created a class module with the following code to make a command button:

Public Sub NewButton(name As CommandButton, sname As String, title As String, w As Integer, h As Integer, x As Integer, y As Integer)
Set name = Form1.Controls.Add("vb.commandbutton", sname)
name.Caption = title
name.Width = w
name.Height = h
name.Left = x
name.Top = y
name.Visible = True
End Sub


Then inside my plugin I can put
BOS.NewButton button, "button", "Plugin Button", 1000, 1000, 0, 0

How can I make the click event inside of the plugin?

kassyopeia
08-11-2009, 05:16 PM
For the wrapper class to receive events from the control, it needs a persistent WithEvents reference to the control. Like so:

Private WithEvents myNewButton As CommandButton

Public Sub NewButton(name As CommandButton, ...)
...
Set myNewButton = name
End Sub

Private Sub myNewButton_Click()
End Sub

However, this is probably not quite what you want, because each time you call the procedure, the reference to the previous button is overwritten. So, you'll need to go one step further and create a wrapper for each button, each of which forwards events to the generator class. Like so (untested):

Private myNewButtons As Collection

Public Sub NewButton(name As CommandButton, ...)
...

Dim newWrapper As ButtonWrapper
Set newWrapper = New ButtonWrapper
Set newWrapper.parent = Me
Set newWrapper.button = name
Call myNewButtons.Add(newWrapper)

End Sub

Public Sub buttonClick(button As CommandButton)
End Sub


'class ButtonWrapper

Private myParent As 'generator class
Private WithEvents myButton As CommandButton

Public Property Set parent(RHS As 'generator class)
Set myParent = RHS
End Property
Public Property Set button(RHS As CommandButton)
Set myButton = RHS
End Property

'event forwarding
Private Sub myButton_Click()
Call myParent.buttonClick(myButton)
End Sub

For an explanation of the approach, see Control/Class arrays - WithEvents! (http://www.xtremevbtalk.com/showthread.php?t=137599)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum