Click to See Complete Forum and Search --> : Adding an ActiveX Control on the fly


David Cowan
May 27th, 1999, 10:19 AM
Is there a way to load an activex control on the fly. I have a control called address that I am playing around with and I need a way to programmatically load it like you would a tree control or listview

Thanks

Ravi Kiran
May 28th, 1999, 02:44 AM
Hi,

Do we *really* load Listviews & TreeViews "on the fly"?

They are first referenced or added to the project using apppropriate OCX or tlbs. Then an instance is created on the form. Only 2nd instances can be loaded on the fly

The process is a easy because we find them in the set of registered controls. If your "address" control is not found in the regis'ed. list on "Project->Controls" or (Cntrl+T), use "Browse" btn that appers next to it; Locate the directory, where the OCX is installed and add it to the list. VB will register it for you and next time you will find it there. You can also use the "Active X Test Container" that comes with VC++ to register.

There is actually a "ultra crazy" but perfectly "on the fly" method of adding controls. I chanced upon it only yesterday... You need to know the CLSID of that control and it should have been registered on the system: and it uses WebBrowser... I will make another post out of it:-)

Ravi

Rehan Shamsi
May 29th, 1999, 04:30 AM
Hi

The control to be used should be registered with your VB project (Project -> Components menu). You can try using the code similar to the one given below for your address control (replace CommandButton with the type of control you want to use):


option Explicit
' Declare CommandButton object.
private withevents cmdObject as CommandButton

private Sub Form_Load()
set cmdObject = Form1.Controls.Add("VB.CommandButton", "cmdDyna1")
cmdObject.Visible = true
cmdObject.Caption = "Dynamic CommandButton 1"
End Sub

private Sub cmdObject_Click()
print "This is a dynamically added control"
End Sub





Rehan

David Cowan
June 1st, 1999, 08:27 AM
The problem I am having is finding out the name of the control. were button is VB.CommandButton how would I find out the name of my control once it is created.

Rehan Shamsi
June 1st, 1999, 12:44 PM
In the VB code line below:

set cmdObject = Form1.Controls.Add("VB.CommandButton", "cmdDyna1")




cmdObject is the reference to your object that is the CommandButton here. Its actual name is "cmdDyna1" which the 2nd parameter supplied to the Controls.Add function. Which means that you can access your control like:

debug.print Form1.Controls("cmdDyna1").Caption





The first parameter to the Controls.Add function is the ProgID of the control. You can try viewing your address control with the VB's Object Browser. You will have to select your Library to view the classes it contains. So you can create your address control like (considering "AddressLib" is the library name and "Address" is the control class):


Dim ctlAddress as AddressLib.Address
set ctlAddress = Form1.Controls.Add("AddressLib.Address", "MyAddress")
Debug.print Form1.Controls("MyAddress").Name 'This should print "MyAddress"




"MyAddress" will be the name of your address control in the above sample.
Hope this helps...