Solved

how to access events from late bound controls [Updated]

Posted on 2001-08-22
7
228 Views
Last Modified: 2008-01-09
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
Comment
Question by:ddoplguy
7 Comments
 
LVL 39

Expert Comment

by:appari
ID: 6413296
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
 
LVL 6

Expert Comment

by:andyclap
ID: 6413344
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
 
LVL 1

Expert Comment

by:falkor
ID: 6414894
'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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 39

Expert Comment

by:appari
ID: 6415964
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
 
LVL 6

Accepted Solution

by:
andyclap earned 200 total points
ID: 6417006
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
 
LVL 16

Expert Comment

by:twalgrave
ID: 7707827
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
 

Expert Comment

by:modulo
ID: 7758338
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Input past end of file vbs script 9 81
VBA: perform index match based on config sheet with two additional parameters. 9 54
Add and format columns in vb6 7 46
Help me. 3 45
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

911 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

16 Experts available now in Live!

Get 1:1 Help Now