Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 167
  • Last Modified:

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
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
vsvb
Asked:
vsvb
  • 5
  • 2
  • 2
1 Solution
 
RanjeetRainCommented:
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
 
RanjeetRainCommented:
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
 
EDDYKTCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
vsvbAuthor Commented:
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
 
EDDYKTCommented:
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
 
EDDYKTCommented:
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
 
EDDYKTCommented:
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
 
vsvbAuthor Commented:
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
 
EDDYKTCommented:
>>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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now