Link to home
Start Free TrialLog in
Avatar of Rick Becker
Rick BeckerFlag for United States of America

asked on

How do I add an Event Handler at run time in VB6

Greetings,

The question title says it all. I am adding Controls to a form dynamically (at run time) and I need a way to add an Event Handler to the Control as well.

I know that in VB.NET there is a AddHandler function but that function does not seem to exist for VB6 or at least I can't seem to locate it.

BTW, I am Not creating an array of Controls so the 'Events' are not available (exposed) through that mechanism.

Thanks for any help you can give

rrbecker
Avatar of jimbobmcgee
jimbobmcgee
Flag of United Kingdom of Great Britain and Northern Ireland image

What about

    Dim WithEvents newControl As New Text1

then using Text1's events?

J.

Avatar of Mike Tomlinson
Basically you can't.  VB6 uses control arrays to accomplish this.  The WithEvents keyword only allows you to handle ONE control.  It is possibly to create a dynamic control and assign it to a variable declared using WithEvents but you can only create ONE dynamic control in this way.  You must have a declaration for each "dynamic" control which makes it pretty silly and you might as use a control array since you must plan ahead at design time.
Avatar of Rick Becker

ASKER

Hi Idle_Mind

Ok, how about this. I have a structure that contains a Command Button (SSCommand).
I create an array of these structures and eventually add the Command Button to a Form.
How do I, (or can I) get at the 'Button_Click()' event for each command button in the structure?

-------example structure------------------
Type ItemDataStructure
    ItemIndex As Integer
    ItemNumber As Integer
    'ItemName As String
    ItemGroupName As String
    ItemButton As SSCommand <<-- Infragistics command button
    ButtonName As String
    ParentName As String
    ButtonWidth As Integer
    ButtonHeight As Integer
    ButtonTop As Integer
    ButtonLeft As Integer
    ButtonOrder As Integer
End Type
------------------------------------------------

Any Ideas?

BTW, I need to step out for a while, be back later today

rrbecker
If the control supports it then you would need to create a control array of SSCommand.  When you create a new structure, also load another button into the control array.  Then instead of storing a button reference in your structure, you store the INDEX of the button in the control array.
Hi Idle_Mind,

I'm back.... I've found that I can create and add the SSCommand button to a  form or panel by using the following syntax:

------------snippet--------------------
MainStruct.GroupStructure(GroupCounter).ItemStructure(ItemCounter).ItemButton.Container = MainStruct.GroupStructure(GroupCounter).GroupItemPanel
-------------------------------------

and this would display a new SSCommand button on my form. I can set all the other properties including Height, Width, Top and Left but I guess I still have the problem of being able to 'Read' the 'Click' event of the button.

If you have any other ideas I would appreciate it, otherwise I'll revert back to a 'Control' array and close this ticket.

Thanks in advance

rrbecker
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Idle_Mind,

Not the answer I was 'Hoping' for but the 'Correct' answer none-the-less. You have helped me with problems in the past and have always given me the 'Best' solution possible.

So again, thanks for all your help, I will proceed to implement the 'Control Array'.

rrbecker