Link to home
Start Free TrialLog in
Avatar of vsvb
vsvb

asked on

Problem With Dynamic Loded Control Array

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
https://www.experts-exchange.com/questions/10303725/Array-of-Private-WithEvents-controls-How.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
Avatar of RanjeetRain
RanjeetRain

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


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.
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
Avatar of vsvb

ASKER

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

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)
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
ASKER CERTIFIED SOLUTION
Avatar of EDDYKT
EDDYKT
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vsvb

ASKER

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
>>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