Solved

Problem With Dynamic Loded Control Array

Posted on 2004-09-27
9
151 Views
Last Modified: 2010-05-02
I am facing one Problem because of Limitaion of VB ..but i am some how hope that
there is solution of this problem

In my programme i am creating some control at run time on form  in that there is one frame
and inside that frame three is label conrolarray. Frame is container of Lable controlArray
now i want to catch the event such like click of lable control .. as i write here before label conrol
is in control array..

problem with event not support controlarray that i have idea but may be it is possible
through VBControlExtender and use of Collection

if you see this example
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_10303725.html
in that there is same type of qustion but the solution is based on UserControl but i am not
work with usercontrol.

this is my code below in this i did for only one control but actually i need that type of
event which fire each time when one of the control from control array is operated..

add one Form , One Module , One Class
## Form1 ##
Private Sub Form_Load()
     Call makecon
End Sub

## Module1 ##

Public ctlFrame As Control, ctlLabel() As Control
Dim cl As Class1
Sub makecon()
     Set cl = New Class1
     Dim n As Integer
     ReDim ctlLabel(3) As Control
     Set ctlFrame = Form1.Controls.Add("VB.Frame", "Toolframe", Form1)
     With ctlFrame
          .Visible = True
          .Left = 0
          .Top = 0
          .Height = 5000
          .Width = 5000
     End With
     For n = 1 To 3
          Set ctlLabel(n) = Form1.Controls.Add("VB.label", "Check" + Trim(Str(n)), Form1)
          Set ctlLabel(n).Container = ctlFrame
          With ctlLabel(n)
               .Visible = True
               .Caption = "xxxx"
               .Top = .Height * n
               .Tag = n
          End With

     Next
     Set cl.ContLabelSet = ctlLabel(1)
End Sub

## Class1##
Private WithEvents ContLabel As Label
Public Property Set ContLabelSet(obj As Object)
   Set ContLabel = obj
End Property

Private Sub ContLabel_Click()
   Debug.Print "click On Me"
End Sub

i hope there is some solution of this prob
0
Comment
Question by:vsvb
  • 5
  • 2
  • 2
9 Comments
 
LVL 19

Expert Comment

by:RanjeetRain
ID: 12160230
The solution to your problem is relatively simple.

You just need to use the correct syntax of the Event Handler. It should read like this:


Private Sub ContLabel_Click(Index as Integer)
  Debug.Print "You clicked on the agent with the index " + ctr(index)
End Sub


0
 
LVL 19

Expert Comment

by:RanjeetRain
ID: 12160325
THat is how you can process a control Array in VB. When this event fires, it is passed the value os its index. Using that, you can get to know which control in the array has caused this event to fire.
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 12160607
Try this, use array of class

Dim cl() As Class1
Dim count1 As Integer
Sub makecon()
    ReDim ctlLabel(3) As Control
    ReDim Preserve cl(3)
    Set ctlFrame = Form1.Controls.Add("VB.Frame", "Toolframe", Form1)
    With ctlFrame
         .Visible = True
         .Left = 0
         .Top = 0
         .Height = 5000
         .Width = 5000
    End With
    For n = 1 To 3
         Set ctlLabel(n) = Form1.Controls.Add("VB.label", "Check" + Trim(Str(n)), Form1)
         Set ctlLabel(n).Container = ctlFrame
         With ctlLabel(n)
              .Visible = True
              .Caption = "xxxx"
              .Top = .Height * n
              .Tag = n
         End With
        Set cl(n).ContLabelSet = ctlLabel(n)
    Next

End Sub
0
 
LVL 3

Author Comment

by:vsvb
ID: 12161018
hi ranjeetrain
if you see my done code the issue is to assign my each control array element on seperat variable
then and then it fire its events perticularlly
if you try the answer you wrote you will get error .... pls take a look second time
tx for you time

hi eddykt
the solution which you gave me the first problme in that
you Not can
Set cl(n).ContLabelSet = ctlLabel(n)
and second i like to catch that event of perticular label clicked on class lavel how can i catch that ??
i think you understand what i mean to say tx

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 26

Expert Comment

by:EDDYKT
ID: 12161430
Change

Set cl(n).ContLabelSet = ctlLabel(n)

to

Set cl(n) = New Class1
Set cl(n).ContLabelSet = ctlLabel(n)


Also change

Public Property Set ContLabelSet(obj As Object)

to

Public Property Set ContLabelSet(ByVal obj As Variant)
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 12161491
If you can to catch the particular, then

In you class, add the method like

Private m_Num as integer
Public sub GetNumber(byval num as integer)
m_Num = num
end if

Private Sub ContLabel_Click()
  Debug.Print "click On Me " & m_Num
End Sub


In you module

Set cl(n) = New Class1
Set cl(n).ContLabelSet = ctlLabel(n)
cl(n).GetNumber n
0
 
LVL 26

Accepted Solution

by:
EDDYKT earned 400 total points
ID: 12161542
Form:

Option Explicit
Private Sub Form_Load()
    Call makecon
End Sub

Module:

Option Explicit
Public ctlFrame As Control, ctlLabel() As Control
Dim cl() As Class1
Sub makecon()
   ReDim ctlLabel(3) As Control
   ReDim Preserve cl(3)
   Dim n As Integer
   Set ctlFrame = Form1.Controls.Add("VB.Frame", "Toolframe", Form1)
   With ctlFrame
        .Visible = True
        .Left = 0
        .Top = 0
        .Height = 5000
        .Width = 5000
   End With
   For n = 1 To 3
        Set ctlLabel(n) = Form1.Controls.Add("VB.label", "Check" + Trim(Str(n)), Form1)
        Set ctlLabel(n).Container = ctlFrame
        With ctlLabel(n)
             .Visible = True
             .Caption = "xxxx"
             .Top = .Height * n
             .Tag = n
        End With
       Set cl(n) = New Class1
Set cl(n).ContLabelSet = ctlLabel(n)
cl(n).GetNumber n

   Next

End Sub


class:

Option Explicit

Private WithEvents ContLabel As Label
Private m_Num As Integer
Public Sub GetNumber(ByVal num As Integer)
m_Num = num

End Sub

Private Sub ContLabel_Click()
 Debug.Print "click On Me " & m_Num
End Sub

Public Property Set ContLabelSet(obj As Object)
  Set ContLabel = obj
End Property


0
 
LVL 3

Author Comment

by:vsvb
ID: 12169037
hi eddykt
tx a lot actually i not prefere lots instance of class
but accourding your solution my problem is solved so thanks again
so i am accepting your solution
i facing another prob actually i am raising this question too in EE

on class lavel there is one with event of label and i want to call
procedure which i created in Usercontrol in this event

#Form1##
Option Explicit
Dim k As Class1
Private Sub Form_Load()
   Set k = New Class1
   Set k.ContLabelSet = Label1
End Sub

#class1#
Option Explicit
Private WithEvents ContLabel As Label
Private Sub ContLabel_Click()
   MsgBox "hi"         '*** Here i want to call Procedure of Usercontrol1
                       '    ProUserControl
End Sub
Public Property Set ContLabelSet(obj As Object)
  Set ContLabel = obj
End Property

#UserControl1#
Public Sub ProUserControl(para As String)
      MsgBox para
End Sub
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 12169200
>>tx a lot actually i not prefere lots instance of class

That's how natural of OO program works. Too bad if you don't like it
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

758 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