I am using this code to load one object from an array of command buttons:
VB Code:
'This is a global variable
Dim cmdNew(4) As CommandButton
Set cmdNew(0) = Me.Controls.Add("VB.CommandButton", "cmdButton" & Me.Controls.Count)
With cmdNew(0)
.Left = 1000
.Top = 1000
.Width = 2000
.Height = 500
.Caption = "Woof"
.Visible = True
End With
Set cmdNew(0) = Me.Controls.Add("VB.CommandButton", "cmdButton" & Me.Controls.Count)
This code will make one commandbutton on the form. But is it also possible to write code for the events for this command button. I tried write this:
VB Code:
Private Sub cmdNew_Click(Index As Integer)
cmdNew(0).Caption = "Nese"
Command1(1).Caption = "Dust"
End Sub
but it doesn't work.
My other question is what is the parameters for the add function I have used. I found the line of code in a book. But it didn't tell me what the parameters what. Anyone know?
My last question is. Is cmdNew here the same or more or less the same as a pointer in C++ or any other language?
Thanks, yes that worked. But if I am making a controll array at design time, and then loads objects, then I can write event code. So I guess that VB is doing it some other way. Do anyone know if this is possible for us to do too? Or is it just a secret for the compiler?
You can write event code for an existing control array, but you cant write code for individual controls (or arrays) that dont exist.
The usual way of doing it is to create one element of a control array at design time, and set the visible property to False (and any other properties to the default you want). You can then add more controls to the array at run time by doing this:
VB Code:
Load txtName(1) '(assumes array is called txtName, and you are creating element 1).
With txtName(1)
.Left = x
.Top = y
.Visible = True
...
End With
In the events for the control array there is an Index parameter which you can use to determine which element was used, so you can run the appropriate code.
Yup....I know....done that the last 5 years...,but this time I was trying to do it completly at run time. But it looks like I can't get the best from both worlds, unless I am using subclassing....
Originally posted by MartinLiss Isn't it easier/better to just create the (0) member of the control array at design time and hide it until needed?
Yeah, but that is what I have done the last 5 years. It was actualy an other person that asked me how you loaded objects ar run time. And I started to play with it. And I found several ways to do it. But I couldn't fugure out how to make an array of controlls at run time, and write event code for them. So there is actually no question about an other way. I know more then one other way. Just want that way to work...
Originally posted by Wokawidget Here you go.
I'll let you work out what uMsg values are for what events
Woka
Of course you had an example.......I will have a look at it after I have eaten. Right now I am trying to make drop down meny for a web site, and I have never done Java script before...:d