can u add code to objects created on the fly?

yyiiyyii
12-31-2007, 04:17 AM
can u add code to objects created on the fly?

For example if i run the following code then i want to run some code if a command icon is clicked.

Static X As Integer
X = X + 1
Set Commandbject = Me.Controls.Add("VB.CommandButton", "CommandButton" & X)
With CommandObject
.Caption = "Name"
.Top = 500
.Left = 500
.Height = 500
.Width = 500
.Visible = True
End With

the master
12-31-2007, 04:22 AM
I dont think its possible that way. I think it might be possible to add events using the script control but i dont know much about that.

By the looks of things you would be better with a control array. You create a control on your form and set the .index property to 0. Now you can load objects like this.

'Assuming your first one is command1

load command1(1)
command1(1).visible=true

load command1(2)
command1(2).visible=true


Notice they are all called "command1" but they have different indexes. You can add your events at design time but now they will have an index passed to them


private sub command1_click(index as integer)

end sub


You can use select case or if statements to check which command button was clicked and do whatever needs doing

dilettante
12-31-2007, 08:54 AM
You can do it but it is clumsy and has limitations. The general idea is to declare an object of the control type or of VBControlExtender WithEvents and set it to point to the newly created control.

This is covered in the VB6 documentation under the Controls collection Add method.

yyiiyyii
12-31-2007, 07:02 PM
do u have a sample u can post here

the master
12-31-2007, 07:11 PM
You can dim with events but i dont think you can do it in the way you want. Not without an array but dim with events doesnt work for arrays anyway and if it did then you might aswell use a control array.


dim tb withevents as textbox

private sub tb_change()

end sub


Theres no point it doing that really. You might aswell put the control on the form from the start

Roscoe
01-01-2008, 11:20 PM
Sounds like you want to do something similar to assigning a macro to a custom toolbar button in MS office. If so, perhaps you could use the Toolbar Control. It is easy to add additional buttons using code at runtime, and the event is already intrinsic. You could keep user selected names and/or keys in an array or collection which you could iterate through in the Toolbar_ButtonPress event, which sends the Button object as an argument.

the master
01-01-2008, 11:23 PM
You could keep user selected names and/or keys in an array or collection...

Theres no need for an array or collection. The toolbar control allows you to specify a key for each button.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum