Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Creation of Control Array 3

Status
Not open for further replies.

CmPaiva

Programmer
Nov 13, 2002
124
PT
Hi, guys.

I'm playing around of dynamic component creation at runtime.
I know how to do this, by two ways:
- Add at design time, a control element with index=0 and for a new instance: Load <ctrl>
- The other way, is dynamicaly create the control by adding a reference to control collection.It's a bit more complicated, but it does'n need the dummy design time control on the form/class.

What i'm asking is that i can't figure how to create a control array, in this way! The idea is to create a class that, when instantiated on a existing form, will create, without any references in design time, a huge matrix of buttons/panels, which configuration/layout will only be known at run time.
The idea of this, is to implement it over a vast number of forms using a centralized environment (created by the class).

If anyone ever do this (if it is possible to be done), I would apreciate a hint.

By the way, i left here, a link that clearly explains dynamic control creation, for those who never had ear about this.


Thanks in advance,
Carlos Paiva
 
See thread222-421131 Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Sunaj,
Thanks for your fast answer.

I did read the threat in question, before i post mine.
The question is:
How do i access object events?

If I declare

Private WithEvents MyCtrl as VBControlExtender

Where do i get the control index, in the event handler?

 
A Big, Big, Big Thanks to you, cos this is exactly what I'm looking for. A great search job that i didn't acomplish with sucess.

Thanks again for your great help.

Carlos Paiva
 
CmPaiva

I am glad that sunaj has solved your problem, but I would like toknow why you decided to use this method over using a &quot;standard&quot; control array? Are there significant advantages, as it seems like a lot of code to replace 1 dummy control!

I'm just interested!

Matt
 
mattKnight

The idea behind that, is to obtain a way in which, at run time, recreate a matrix of controls, using a general class to Create Layout/Configuration.
Till now, everything is ok on using a dummy control.

The problem comes, when such control, is not always the same control.Even more, in the same form I want a matrix of related controls,(related by index), of different types.

I'm not sure if i've been explicit enought.

Sunaj,

I try the idea in the related link, but i still didn't managed it to work.It seems that something is missing, but i'm trying my own code, using the idea. When i get it to work, i will post it here as a Tip as a way to be used by everybody.

Thanks to all of you, and sorry about my not so good english.

Carlos Paiva
 
CmPaiva I'll be interested to see a code example.
Don't be sorry about the english - I read you loud and clear Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
CmPaiva

I think I understand now!

> when such control, is not always the same control

I assume that is a different type of control, Are the types changing dynamically?

Perhaps another solution would be to create a usercontrol consisting of, say, 1 row worth of the required array, the required events could then be exposed.

using a control array would then be possible!

But I am sure you have the solution that works for you!

I agree with sunaj I can understand well enough!

Take care

Matt
 
Hello, all!
It's working...

Now the code:
-Usage: in the final desired form(Eg. Form1)

'====================================
'= Main form named: Form1
'====================================
'In here, our Control Collection is declared (withevents),
'to emulate array functionality.
'====================================
Option Explicit
Private WithEvents LabelArray As CLabels

Private Sub Form_Load()
Set LabelArray = New CLabels
Set LabelArray.Parent = Me
LabelArray.CreateArray 16
End Sub

Private Sub LabelArray_Click(ByVal Index As Integer)
MsgBox (&quot;Hello &quot; + CStr(Index))
End Sub

'====================================
'= Class module named: CLabel
'====================================
'This is the actual control behabiour
'In here, among with the control related members,
'I added a new one to store a reference to the central
'event object, in the colection (mEvPub)
'====================================
Option Explicit

Private WithEvents mCtl As Label
Private mParent As Form
Private mEvPub As CEvPublish
Public Event Click(ByVal Index As Integer)

Public Property Set Parent(frm As Form)
Set mParent = frm
End Property

Public Property Set EvPublisher(Ev As CEvPublish)
Set mEvPub = Ev
End Property

Public Sub gDrawOnForm(ByVal Name$, ByVal Top%, ByVal Left%, ByVal Height%, ByVal Width%)
Set mCtl = mParent.Controls.Add(&quot;VB.Label&quot;, Name$)
Set mParent = Nothing 'Important Line of Code
mCtl.AutoSize = False
mCtl.Caption = mCtl.Name
mCtl.BackStyle = 1
mCtl.Top = Top
mCtl.Left = Left
mCtl.Height = Height
mCtl.Width = Width
mCtl.Visible = True
End Sub

Private Sub mCtl_Click()
mEvPub.Clicked Mid(mCtl.Name, 5)
End Sub

'====================================
'= Class module named: CLabels
'====================================
'This is encapsulation of specific collection
'In here, among with the collection related members,
'I added a new one to store a reference to the
'central form, (mParent) and the main
'event trigger object(mEvPub)
'====================================
Option Explicit
Public Event Click(ByVal Index As Integer)
Public mParent As Form
Private mCol As Collection
Private WithEvents mEvPub As CEvPublish

Public Property Set Parent(frm As Object)
Set mParent = frm
End Property

Public Sub CreateArray(iSize As Integer)
Dim n As Integer, mLbl As CLabel
Set mCol = New Collection
Set mEvPub = New CEvPublish
For n = 0 To iSize - 1
Set mLbl = New CLabel
With mLbl
Set .Parent = mParent
Set .EvPublisher = mEvPub
mLbl.gDrawOnForm &quot;LblT&quot; & n, (n Mod 4) * 300, (n \ 4) * 900, 300, 900
mCol.Add mLbl
End With
Next
Set mLbl = Nothing
End Sub

Private Sub mEVPub_Click(ByVal Index As Integer)
RaiseEvent Click(Index)
End Sub

'====================================
'= Class module named: CEvPublisher
'====================================
'This implements an event fireing mechnism.
'====================================
Option Explicit

Public Event Click(ByVal Index As Integer)

Public Sub Clicked(ByVal Index As Integer)
RaiseEvent Click(Index)
End Sub
'===================================================

And that's all!
Remember that this is not, only my work, but an adaptation of Christian Landesberger original code described in the above link!

I hope that this code i post can help any of you

Thanks for helping me in get things work.

Carlos Paiva
 
Hi, CmPavia

I tried your code and is works great [thumbsup] Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top