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.
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
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.