Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Default Initializing Code of Control

Is it possible to get the default init code of a control by using TypeLib?

Suppose: If I put one TextBox onto the form then I get this coding as default.

Begin VB.TextBox Text1
Height = 495
Left = 1740
TabIndex = 1
Text = "Text1"
Top = 1305
Width = 1215
End


Actually, I'm creating one program that can be generated the form with a particular control.In that program, a User can specify the ocx that is wanted to put onto the form and then, Click "Generate" button to generate one form with desired control.

Any help would be appreciated.
Thanks in advance.
[746 byte] By [Sync] at [2007-11-11 7:54:22]
# 1 Re: Default Initializing Code of Control
I am not so sure I understand.

Do you want to generate the "code" of a form, that will be later compiled, or do you want to add a control (ocx file I guess) to your (pre-compiled) form at run-time?
Anyhow, a type library refers only to ActiveX objects (ocx included), and gives you informations about the "interfaces" exposes by the public modules.
The "default" parameters of a control added at design time depend on the control itself.
Can you be more specific (unless some else understood you already)

Marco
mstraf at 2007-11-11 17:26:18 >
# 2 Re: Default Initializing Code of Control
Thanks. macro.
Actually, I'm creating one samll program that can be generated .vbp and .frm.

Steps:
1) Open the VB6 and Get one Standard EXE Project (Name of project : "VBProjectGenerator").
2) Put two buttons ("btnBrowse" and "btnGenerate") and one OpenFileDlg onto the form.
3) Writing the coding.
4) Run the program
5) Click "Browse" button. (OpenFileDlg will show.)
6) Select one OCX ("MyTextBox.ocx"). //MyTextBox.ocx can be any custom component
7) Click "Generate" button.

This program will generate one .frm with the control. and It will also generate one project file (*.vbp) with the control as reference.
After generating, the generated project can be opened with Visual Studio IDE as like as normal project.
I have finished to create for generating VB Project file (*.vbp).
I have problem in generating .frm.

Any suggestion/idea would be appreciated.
Thanks!
Sync at 2007-11-11 17:27:17 >
# 3 Re: Default Initializing Code of Control
I can try once more if anyone didnt clear what I like to ask.
Please!
Sync at 2007-11-11 17:28:16 >
# 4 Re: Default Initializing Code of Control
I don't like the idea of generating a frm and vbp files, too many things that can go wrong.
Why don't you create a standard exe, and load the control at run time using the Controls.Add method? It is much easier, and you do not have to recompile anything.

Marco
mstraf at 2007-11-11 17:29:23 >
# 5 Re: Default Initializing Code of Control
Thank you. mstraf.
Actually, we are doing setup automation (component installer) so that we really need to test whether the control can be placed on the form or not after installation finished. It's difficult to do referencing the required ddls, adding required ocx to the VB6 toolbox and putting this ocx onto the form from the other program. So, I think it might be more easier to generate the .vbp that already has reference required libraries and .frm that already has the control.
Please let me know if you have any idea for better way.
Sync at 2007-11-11 17:30:22 >
# 6 Re: Default Initializing Code of Control
Begin VB.TextBox Text1
Height = 495
Left = 1740
TabIndex = 1
Text = "Text1"
Top = 1305
Width = 1215
End

This is the default code for TextBox whenever you put it onto the form.
I like to get the coding as like as the following ~
eg:

Dim strResult as String
strResult = GetDefaultInitCode("C:\TextBox.ocx")
Debug.Pring strResult

----------
Output :
Begin VB.TextBox Text1
Height = 495
Left = 1740
TabIndex = 1
Text = "Text1"
Top = 1305
Width = 1215
End

If component developer will add the new property to this control,
GetDefaultInitCode(String) function should return the result including
new property.
Any Idea?
Thanks in advances.
Sync at 2007-11-11 17:31:21 >
# 7 Re: Default Initializing Code of Control
Thank you. mstraf.
Actually, we are doing setup automation (component installer) so that we really need to test whether the control can be placed on the form or not after installation finished. It's difficult to do referencing the required ddls, adding required ocx to the VB6 toolbox and putting this ocx onto the form from the other program. So, I think it might be more easier to generate the .vbp that already has reference required libraries and .frm that already has the control.
Please let me know if you have any idea for better way.

This is what I do.
I created a project with only one form, nothing in it (just a textbox to display error messages). No references, nothing.

When the program starts, it scans a selected folder (where the ocx are) and reads the typelib of each ocx file, getting the GUID and PROGID of every public components. If it fails, it writes one or messages in the TextBox. Otherwise it tries to create a control like this (it is just a snippet from a bigger application)

private sub AddControl(controlGuid as string)
on error goto errsub
Licenses.Add controlGuid
me.controls.add controlGuid, "ctl"
Me.Controls.Remove "ctl"
exit sub
errsub:
''' any messages you want, but do not forget the controlGuid string
exit sub

The nice thing of the TextBox if that you have something to cut and paste for reference, without annoying Message Boxes. Handy for customers that want to email you back the error messages.

Marco

PS: if a control does not load, that does not necessary means that there is something wrong with that control, but it is possible that the controls is referencing other controls or dll's that cannot be loaded. So, the quest for falling angels :) is not that easy. Good luck...

PPS if your controls have a license key, then the code above needs some changes.
mstraf at 2007-11-11 17:32:24 >
# 8 Re: Default Initializing Code of Control
Thank you so much for your suggestions. mstraf.
Sync at 2007-11-11 17:33:24 >