Assigning events to runtime-created controls - is it possible?

B

BizMark

I'm planning to write a workbook with a suite of macros and 'plan'
userforms which use data from 'outboard' workbooks to present that data
within them, as forms.

As part of this I want to be able to specify 'form' templates that
depending on the 'field' types chooses to display each 'record' (i.e.
row) as a series of edit boxes, dropdowns, combos etc. according to
what is relevant for the type of data in each field.

Rather than design lots of different permutations of forms for this
purpose, I want to be able to create each form dynamically through
code, so that the source data determines how the form will look and how
many controls it will have.

So far, I have no problem with this - I have added controls through
code many times and am fairly comfortable with that side of things.

HOWEVER - this is the big sticking point - I want to be able to assign
EVENTS to these dynamically created controls, and I can't find a way to
do it. Remember that I don't want to have to assume what all the
control names are going to be and when they are going to present or
not. Therefore, I don't want to have to 'pre-write' event code for
every possible control name that there will ever be.

What I want to be able to do however is write some 'generic' event
routines, that perhaps I can attach to invisible or 'off-form' controls
of each given type that I will use (so that parameter declarations
compile OK) and then 'duplicate' these for new controls at runtime. I
keep telling myself this is a solution that surely must have been
required by many programmers, but I can't find any help on this topic,
and all documentation about event routines seems to point to having to
create the control first, then write the event code second.

It was simple with Dialogsheets - simply setting the .OnAction property
to any macro you like - although of course you didn't get
system-generated parameters sent to the routine to work with like
events have.

So, is there a way of doing this with UserForm controls? If so, does
it involve lots of Declares of DLLs and stuff and registering them? I
hope not.

Anyone with a solution will receive my sincerest appreciation!

BizMark
 
B

BizMark

From the complete lack of replies this seems to confirm my worst fears -
that there is no 'proper' way of doing this. I guess this has everyone
stumped?

Well, I haven't got time to try it right now, but it looks like I'm
going have to go back to my Pascal/Omnis 7 roots and go down the route
of a 'Window Control Procedure' which runs all the time the window
(userform) is active - i.e. in the 'Activate' event procedure, I'll
have it running an endless 'DoEvents' loop that checks the Focus and
then jumps to routines accordingly, those routines taking the place of
event procedures and reading the UserForm.ActiveControl property to
determine what control to operate on.

Finally I would simply check if the ActiveControl is any button that's
intended to dismiss the form and .Hide it from within the .Activate
event procedure.

Should work, shouldn't it?

MB
 
Top