Creating Controls at Runtime with MS Scriptcontrol, don't fire Events

vbfan
09-22-2003, 09:35 AM
When I create controls at runtime with the help of the MS Scriptcontrol.
My created Controls(e.g. a Commandbutton) doesn't decteted when he was ie clicked.
I use this kind of code to create the Controls.

Dim WithEvents NewCmdButton
dim NewCmdButton
Set NewCmdButton = frmScriptbox.Controls.Add("VB.CommandButton", "NewCmdButton")
NewCmdButton.Top = 1000
NewCmdButton.Left = 1000
NewCmdButton.Height = 1000
NewCmdButton.Width = 1000
NewCmdButton.Visible = True

Private Sub NewCmdButton_Click()
MsgBox "NewCmdButton_Click()"
End Sub

I'm actually start thinking of that's not possible to create really new controls at runtime through Ms ScriptControl.
Or do I have to execute the Code(1 paragraph) in some Sub that it will work?

zak2zak
09-22-2003, 10:22 AM
May be this helps....

'DECLARATION TOP LEVEL
Dim WithEvents NewCmdButton As CommandButton
---------------------------------------
'IN OTHER PROCEDURE SUCH AS FORM_LOAD()
Set NewCmdButton = frmScriptbox.Controls.Add("VB.CommandButton", "NewCmdButton")
NewCmdButton.Top = 1000
NewCmdButton.Left = 1000
NewCmdButton.Height = 1000
NewCmdButton.Width = 1000
NewCmdButton.Visible = True

Private Sub NewCmdButton_Click()
MsgBox "NewCmdButton_Click()"
End Sub

vbfan
09-22-2003, 11:57 AM
Sorry had forgot to say that's not allowed to declare a var as something.
All vars you use in your Project are as variant or such datatype.
When you use as In your Code it brings an error.
But thanks anyway.

Peperl
09-22-2003, 11:59 AM
Ummm ... Perhaps it works with a class module ... ?

vbfan
09-22-2003, 12:37 PM
Sorry I doesn't understand what you mean by using a class modul.
Can you explain it a bit more, please?

Peperl
09-22-2003, 05:10 PM
Well, I'm Spanish, and I'm not speak very well at English but ... when I an module class you could create, for example, an object at running time and apply it Events like Click, GotFocus, etc.

I have some code in the computer where I works. So, tomorrow I'll send you some code that use its. But sure you can find tutorials in Internet, on in other threads on this forum.

See you soon

Peperl
09-23-2003, 02:21 AM
Hi again. Look this example, this will create a TextBox collection, and all of them, when get the focus, the color will change.

First, you have to add a Class Module in Visual Basic and save it as MyClassModule, for example:

Private form_selected As Form
Private WithEvents MyTextBoxControl As TextBox
Private MyObjets(100) As Clase_enfoque ' I think this line is in the class module. you define taht you can create a max of 100 (for example) MyTextBoxControls

Public Sub Create(element As Object, formulario As Form)

' If the control (element) we pass to the module is a Textbox
' then will add this textbox to MyTextBoxControl

If TypeOf element Is TextBox Then Set MyTextBoxControl = element

' To know where is the control who calls this function
Set form_selected = formulario

End Sub

And then, you can define the event inside the Class Module ...

Private SUB MyTextBoxControl_GotFocus()
With MyTextBoxControl
.BackColor = vbYellow
End With
END SUB

Private SUB MyTextBoxControl_LostFocus()
With MyTextBoxControl
.BackColor = vbWhite
END With
END SUB


Ok ... then, you have to said to Visual Basic that the TextBox that you have in a form, you want to add it to MyTextBoxControl class.

' We create a new instace of the class

Set MyObjets(1) = New MyClassModule
MyObjetcs(1).Create TextBox1, Me


I hope this works. If you don't understand it, I tried to pass you an example of this in Visual

Peperl
09-23-2003, 02:25 AM
Ey sorry. Where I put Clase_enfoque it will be MyClassModule.

vbfan
09-23-2003, 03:03 AM
Thanks for the Code I will have a look at it.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum