Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Pro VB 6
|
Pro VB 6 For advanced Visual Basic coders working in version 6 (not .NET). Beginning-level questions will be redirected to other forums, including Beginning VB 6.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 25th, 2005, 03:29 PM
Registered User
 
Join Date: Aug 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Events for multiple dynamically created controls

Hi,

I'm new to the forum, so if this is in the wrong section, lemme know and I will post it somewhere else.


I am trying to dynamically create a bunch of labels (command buttons will work also) on a VBA form and capture the click event on all of them. I do not know at design time how many I am going to need. I have no problem creating the multiple labels. It is capturing the click event that is causing problem. Capturing events for the most recently created label is working fine, but not for all of them. Any help would be greatly appreciated.

Thanks,
Puck312
 
Old August 26th, 2005, 07:26 AM
Authorized User
 
Join Date: Jun 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In VB6 this would be possible by setting up the label control as an array of labels (ie. lblTest(0), lblTest(1), lblTest(2), etc)

Unfortunately VBA doesn't support control arrays. To achieve this you'll need to use a technique whereby you sink in to a special contain class the control you have just created.

In other words, create a custom "label" class, one that exposes the Label object's events which you would like to capture (as well as any other properties). Create a new instance of the class for each label you create at runtime, and pass in a pointer to the new label control.

You then will probably want to create a collection class to contain each instance of your custom label class. This will allow you to iterate over the new set of objects more easily.

If none of this makes immediate sense to you, try searching google with something like "control arrays vba" and you should eventually find some examples of the technique


 
Old August 26th, 2005, 12:27 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

if it works for some labels and not other, there could be a problem in your code. Can you post it?
Marco
 
Old August 29th, 2005, 06:32 AM
Registered User
 
Join Date: Aug 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is the code that I have in the form right now. Pretty much like some code that I found in another forum to to see if it would do what I was looking for.

Dim c() As MyCtrlEvents
Dim intLastTop As Integer
Dim intCount As Integer


Private Sub CommandButton1_Click()
   ReDim c(intCount + 1)
   Set c(intCount) = New MyCtrlEvents

   Set c(intCount).MyBox = Me.Controls.Add ("Forms.Label.1")
   With c(intCount).MyBox
      intCount = intCount + 1
      intLastTop = intLastTop + 30
      .BackColor = vbWhite
      .Name = "LabelTest" & intCount
      .Top = intLastTop
      .Width = 72
      .Height = 18
      .Left = 20
   End With
End Sub

And here is the code from the MyCtrl class module:

Option Explicit
Public WithEvents MyBox As MSForms.Label

Private Sub MyBox_Click()
   MsgBox MyBox.Name & " click!"
End Sub


 
Old August 29th, 2005, 10:22 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

the problem is in

   ReDim c(intCount + 1)
   Set c(intCount) = New MyCtrlEvents

you must "Redim preserve", otherwise Redim will create a new array; and I do not see where you increase intCount. Collections are much better than arrays in situation like this. Declare a Collection and set it in you Load event (that is, only once in your code) and then just Add elements to it like

Private Sub CommandButton1_Click()

dim ctl as new MyCtrlEvents
''
'' initliaze ctl here
''
if myCollection is nothing then set myCollection = new Collection
myCollection.Add ctl

Marco
 
Old August 29th, 2005, 03:05 PM
Registered User
 
Join Date: Aug 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Marco,

Problem solved! Thanks for pointing out the Redim problem. I've been using MATLAB for the past couple of years and forgot that dynamically (re)sizing arrays isn't quite as straight forward in VB. I had tried usign a collection earlier and when I went back to look at it, I had the same type of problem with a Dim statement. oops. It's an obvious mistake once it's pointed out to you. Thanks a lot!

Puck312






Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help me about dynamically created controls vingo_mail ASP.NET 2.0 Basics 1 March 24th, 2008 12:54 PM
Problems with Dynamically created controls Preeti_Singh .NET Framework 1.x 1 March 7th, 2007 01:30 PM
Correctly disposing dynamically created controls simarik C# 2 December 20th, 2005 08:49 PM
Dynamically created controls katsarosj ASP.NET 1.0 and 1.1 Basics 4 February 4th, 2004 09:13 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.