Link to home
Start Free TrialLog in
Avatar of kevp75
kevp75Flag for United States of America

asked on

How to add click event to dynamic control...

I have some label controls being added to my form at runtime.  What I need to do now is to add/detect/get the click event for each label.

Anyone know how I can do this?  (I'm a web developer by trade, but really have only dealt in scripting languages...)

Here's my code...
Avatar of kevp75
kevp75
Flag of United States of America image

ASKER

doh!   sorry...forgot the code

here it is, I promise!

Private i As Integer 
Private Sub Form_Load()
    Dim lblPageLinks() As Variant
    For i = 0 To 10
        ReDim lblPageLinks(i)
        Set lblPageLinks(i) = Controls.Add("VB.Label", "lContLinks" & i, Frame1)
        With lblPageLinks(i)
            .Visible = True
            .AutoSize = True
            .Left = 200
            .Height = 300
            .Top = 300 + (.Height * i)
            .Caption = "Page Link " & i
            '.Width = 300
            .FontUnderline = True
            .FontName = "Trebuchet MS"
            .FontSize = 10
            .ForeColor = &HFF0000
            .MouseIcon = LoadPicture(App.Path & "\harrow.CUR")
            .MousePointer = vbCustom
        End With
        Set lblPageLinks(i) = Nothing
    Next
End Sub

Open in new window

The dynamic controls are created as control array

so you can use as lblPageLinks_click()

Based on the index value you can perform task.
Avatar of kevp75

ASKER

Private Sub lblPageLinks_Click()
    MsgBox IsArray(lblPageLinks)
End Sub


does not do anything...
make the breakpoint and check whether control is going there are not.

Private Sub lblPageLinks_Click()
    MsgBox IsArray(lblPageLinks)
End Sub
Avatar of kevp75

ASKER

Ok.  Changed the code a little...

Also, added the breakpoints, and nothing happens

Private i As Integer
Private Sub Form_Load()
    Dim lblPageLinks() As Variant
    For i = 0 To 5
        ReDim lblPageLinks(i)
        Set lblPageLinks(i) = Controls.Add("VB.Label", "lContLinks" & i, Frame1)
        With lblPageLinks(i)
            .Visible = True
            .AutoSize = True
            .Left = 300
            .Height = 300
            .Top = 300 + (.Height * i)
            .Caption = "Page Link " & i
            .FontUnderline = True
            .FontName = "Trebuchet MS"
            .FontSize = 10
            .ForeColor = &HFF0000
            .MouseIcon = LoadPicture(App.Path & "\harrow.CUR")
            .MousePointer = vbCustom
        End With
        Set lblPageLinks(i) = Nothing
    Next
End Sub 

Private Sub lContLinks_Click(Index As Integer)
    MsgBox lContLinks(Index).Text
End Sub

Open in new window

Avatar of kevp75

ASKER

I also tried taking out the & i from Controls.Add line, figuring it would create it as an array, instead of simply 0-5 controls named lContLinks0 - lContLinks5

All that did was error out stating there was already a control with that name...
Avatar of kevp75

ASKER

knowing this.... the (lContLinks0 - lContLinks5) I would say it is not a control array...
ASKER CERTIFIED SOLUTION
Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

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
Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

Well EE never posted the example in reference to the "Consider the following"
Dim Withevents Label1 As Label
Dim Withevents Label2 As Label
Dim Withevents Label3 As Label
 
Set Label1 = lblPageLinks(0)
Set Label2 = lblPageLinks(1)
Set Label3 = lblPageLinks(2)

Open in new window

Avatar of kevp75

ASKER

my concern is that I will never know how many of these labels need to be created.  I will be pulling data from a database to populate this list of labels, and based on the query it can pull different number of records.

I will try the clone method you stated.  Just set the originating label visibility to false so it does not show, and get back with the results...
SOLUTION
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
SOLUTION
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
Avatar of kevp75

ASKER

Works a champ.  Thanks!