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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Create Control Array at Run Time ? 1

Status
Not open for further replies.

Suwito

Programmer
Apr 24, 2001
16
0
0
ID
Hi,

Anyone know how to create control array at run time with Add Method (Controls Collection).
I do not need create that control at design time before.

Regards,
Suwito.
;-)
 
Dim objText As TextBox
Set objText = Me.Controls.Add("VB.TextBox", "txtMyDynamicBox")

With objText
.Visible = True
.BackColor = vbYellow
.ForeColor = vbBlack
End With

....

Set objText = Nothing


A third argument to the Add method allows the dynamically created control to be placed in a container of your choice e.g. a Frame

A comprehensive description of the Add method can be found at: _________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Thank for your attn, but I need create Control Array.

;-)
 
Dim objText(n) as TextBox

For i = 0 To n - 1
Set objText(i) = Me.Controls.Add("VB.TextBox", "txtMyDynamicBox" & i)

With objText(i)
.Visible = True
.BackColor = vbYellow
.ForeColor = vbBlack
End With
Next

One of the issues here is receiving events - you can't Dim WithEvents an array. The way to go about this is to create your own control which does everything a text box does but has one additional property - a notification class

Now the notification class is something else you need to create. It would look something like this:

Event Click(Index as Long)
Event DblClick(Index as Long)
Event ...

Public Sub Click(Index as Long)
RaiseEvent Click(Index)
End Sub

And so on and so forth for all of the events that you are interested in. The important thing is that each Sub has an additional Index parameter.

In you user control now, you can do the following:

Private Sub Text1_Click()
' Assume the notifier class is instantiated and called
' Notifier and that the index for this instace is
' stored in the .Tag property. Unfortunately .Index
' is read-only at run-time
Notifier.Click(Text1.Tag)
End Sub

Now, in your application:

Private WithEvents Notifier as myNotifier

' Dynamically add the control
With oControl
Set .Notifier = Notifier
...
End With

Private Sub Notifier_Click(Index)
' We have received a Click event from instance Index
' of our control

Hope this helps - its a bit of work, unfortunately.

Chaz
 
I had to slightly modify scorpio66's code to make it run: The Dim Statement cannot contain a variable as a dimension.

Here's a corrected version
Dim n As Integer, i As Integer

Dim objText() As TextBox

n = 5
ReDim objText(n)

For i = 0 To n - 1
Set objText(i) = Me.Controls.Add("VB.TextBox", "txtMyDynamicBox" & i)

With objText(i)
.Visible = True
.BackColor = vbYellow
.ForeColor = vbBlack
.Text = CStr(i)
If i <> 0 Then .Left = objText(i - 1).Left + objText(i - 1).Width
End With
Next

Debug.Print Me.Controls.Count

Dim objAny As Object
For Each objAny In Me.Controls
If TypeOf objAny Is TextBox Then
Debug.Print objAny.Name

Debug.Print objAny.Index

End If
Next


The output produced will be:
5
txtMyDynamicBox0


It stops there, because the Red line in the source code will produce an error statement, telling that there is no array!

I'm afraid that to safely implement control arrays you better return to the VB5 way of just loading One Controls of the sort and then to Load the other ones.

Thread222-14902 discusses pro and contras _________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Your correct, the Index property is not set, as it is not an array. What I am proposing provides the same functionality as a control array ...

If you look at my code I actually put the index in the .Tag property, which is not used. In fact, I did not make that clear enough ... :-( There are other ways as well - as you are basically creating a wrapper control around the control that you want to create instances of you can add your own property.

You are right though - Loading is simpler but does have the disadvantage of having to have at least one instance of the control on the form in the first place. I don't know if that is a problem.

Chaz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top