Link to home
Start Free TrialLog in
Avatar of ddoplguy
ddoplguy

asked on

how to access events from late bound controls [Updated]

Im creating an array of labels at run time. not a control array but truly late bound controls using (form).Controls.Add. Because they are created at run time, how can I access any of the events of this control array? Also if the controls are created as part of a UDT.
----
More explaination. I have a UDT that contains a label as one its elements.
Example:
type thing
  a as int
  oLab as Label
end type

dim aThings(num) as thing
'and
redim preserve aThings(newNum)

I need the ability dynamically resize an array of this UDT type at runtime.
I want to be able to handle the events of the label at runtime as if it were a normal control array that was set a design time.

Avatar of appari
appari
Flag of India image

the only way you can access events of controls created at runtime is by defining the control object in the general declaration section and withevents.

like

dim withevents NewText as vb.textbox

but we cannot create control array in this manner.

Hi
This is a bit fiddly, but certainly possible.
When you add a control, you get back a VBControlExtender object which has an ObjectEvent event which will pass the events from the object through in an EventInfo structure.

The main problem is that you can't have an array of objects declared withevents, so it gets a bit fiddly with multiple objects

I put together a detailed comment a while back describing how to do this nicely, so you might like to take a look at:

https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=10303725
'I did this for adding checkboxes to a form
Sub Main()
Dim chkObj As Object
Dim iCtr As Integer
Dim iTop As Integer
Dim iLeft As Integer

iTop = 500
iLeft = 200

For iCtr = 1 To 20
Set chkObj = frmCheck.Controls.Add("VB.CheckBox", "chkObj" & iCtr) 'add the counter for a distinct name
With chkObj
    .Height = 195
    .Width = 600
    .Visible = True
    .Caption = iCtr

    'the following top and left stuff is just to give columns of 5 checkboxes
    .Top = iTop
    .Left = iLeft
    iTop = iTop + 200
    If iTop > 1400 Then
       iLeft = iLeft + 800
       iTop = 500
    End If
End With
Next iCtr
End Sub


'this is the code to look at the checkboxes
For Each Control In Me.Controls
    If InStr(1, Control.Name, "chkObj") Then
        If Control.Value = 1 Then
        'if it's checked, do something

        End If
    End If
Next
andyclap,
i have seen your answer given in the link provided by you. there it was mentioned this is not possible to use with VB intrinsic controls. but ddoplguy wants to implement this on label control. means no other way than creating a custom control and then use that one.
ASKER CERTIFIED SOLUTION
Avatar of andyclap
andyclap
Flag of United Kingdom of Great Britain and Northern Ireland 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
Avatar of twalgrave
twalgrave

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in Community Support that this question is:
- points to andyclap
Please leave any comments here within the
next seven days.
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange