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

Posted on 2005-05-13
Last Modified: 2008-02-01

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

Question by:rrbecker
    LVL 16

    Expert Comment

    What about

        Dim WithEvents newControl As New Text1

    then using Text1's events?


    LVL 85

    Expert Comment

    by: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.
    LVL 2

    Author Comment

    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

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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.
    LVL 2

    Author Comment

    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:

    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

    LVL 85

    Accepted Solution

    As far as I know, you will have to use the control array method.

    LVL 2

    Author Comment

    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'.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
    When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now