Solved

how to access events from late bound controls [Updated]

Posted on 2001-08-22
7
227 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

759 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

19 Experts available now in Live!

Get 1:1 Help Now