Solved

how to access events from late bound controls [Updated]

Posted on 2001-08-22
7
230 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

820 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