Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

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.

0
ddoplguy
Asked:
ddoplguy
1 Solution
 
appariCommented:
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.

0
 
andyclapCommented:
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:

http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=10303725
0
 
falkorCommented:
'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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
appariCommented:
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.
0
 
andyclapCommented:
You can use a similar technique for labels, however rather than maintaining VBControlExtenders for the added controls, you'll need to use VB.Labels, and capture and pass through the individual label events (or at least the ones you're interested in) appropriately.

I can alter my code snippet if you like, ddoplguy - can you let me know if you can do this yourself, or would like me to provide an example.
0
 
twalgraveCommented:
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.
0
 
moduloCommented:
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now