Solved

Problem With Dynamic Loded Control Array

Posted on 2004-09-27
9
161 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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!

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
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…

732 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