easy!! loading a control

Is there any way of loading a control at run time....or putting some piece of code in an application saying...
"load this control if it exists"
Who is Participating?
jackjokerConnect With a Mentor Commented:
Try this.
Let's say you're using TextBoxes.

Make 2 textboxes, visible or not, as you like.
Edit their names to "Text(0)" and "Text(1)"

On the click event of a button, type the following.
  Dim i As Integer
  i = i + 1

  Load Text(i)
  Text(i).Top = 25
  Text(i).Left = 25

  And so on...

To load a control at runtime, you have to have an array of controls, like: Text1(0), Text1(1), Text1(2) and so on...

I don't understand the "load this control if if exists", what do you mean exists? The control or the program, if it is the program, you have to use subclassing, if not, which type of control is it?

- Vidar Braut Haarr
  AmigoSoft Productions
I'm sorry, that was meant as a comment, not an answer.
Though, if you will accept is as an answer, that would be nice.

- Vidar Braut Haarr
  AmigoSoft Productions
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

vbkannAuthor Commented:
What i want to do is load a control to a form that isnt part of a control array...a control that i havent added on at design time.

Because i am using an outlook express .ocx...not every computer has outlook express installed..so if they dont have outlook installed, my program wont run because if performs an error on startup saying that it cant load that control because it is not there.
So i want to somehow say in code, load this control...if it exists.
If you understand what im saying...
I Think you cannnot load a control on a form, if you don't use an array of controls as mannen explained.

What you could probably  do, is to write an Activex.Exe with a form, that is not visible - this active.exe should own the .ocx-control and it should have a fine ErrorHandling for the case, that the server cannot load because the ocx does not exist on the computer where your programm runs.

So, if the .ocx does not exist, you receive an error in your main application, because the activeX-server was not able to instantiate himself correctly or the call to instantiate the server failed - this error you can catch in your main programm.

If the .ocx exists you can use it within the activeX.exe, but not within your main form.
I think my comment is somehow unclear, but your question is NOT easy.


vbkannAuthor Commented:
hmmmm i think you know what i want...but i dont have a clue how to do it
' paste this code to EMPTY form, VB6 only!
Option Explicit
' If you are adding an ActiveX control at run-time that is
' not referenced in your project, you need to declare it
' as VBControlExtender.
Dim WithEvents ctlDynamic As VBControlExtender
Dim WithEvents ctlText As VB.TextBox
Dim WithEvents ctlCommand As VB.CommandButton

Private Sub ctlCommand_Click()
  ctlText.Text = "You Clicked the Command button"
End Sub

Private Sub ctlDynamic_ObjectEvent(Info As EventInfo)
' test for the click event of the TreeView
  If Info.Name = "Click" Then
    ctlText.Text = "You clicked " _
     & ctlDynamic.object.selecteditem.Text
  End If
End Sub

Private Sub Form_Load()
  Dim i As Integer
  ' Add the license for the treeview to the license collection.
  ' If the license is already in the collection you will get
  ' the run-time error number 732.
  Licenses.Add "MSComctlLib.TreeCtrl"
  ' Dynamically add a TreeView control to the form.
  ' If you want the control to be added to a different
  ' container such as a Frame or PictureBox, you use the third
  ' parameter of the Controls.Add to specify the container.
  Set ctlDynamic = Controls.Add("MSComctlLib.TreeCtrl", _
    "myctl", Form1)
  ' set the location and size of the control.
  ctlDynamic.Move 1, 1, 2500, 3500
  ' Add some nodes to the control.
  For i = 1 To 10
    ctlDynamic.object.nodes.Add Key:="Test" & Str(i), Text:="Test" _
      & Str(i)
    ctlDynamic.object.nodes.Add Relative:="Test" & Str(i), _
      Relationship:=4, Text:="TestChild" & Str(i)
  Next i
  ' Make the control visible.
  ctlDynamic.Visible = True
  ' add a textbox
  Set ctlText = Controls.Add("VB.TextBox", "ctlText1", Form1)
  ' Set the location and size of the textbox
  ctlText.Move (ctlDynamic.Left + ctlDynamic.Width + 50), _
    1, 2500, 100
  ' Change the backcolor.
  ctlText.BackColor = vbYellow
  ' Make it visible
  ctlText.Visible = True
  ' Add a CommandButton.
  Set ctlCommand = Controls.Add("VB.CommandButton", _
    "ctlCommand1", Form1)
  ' Set the location and size of the CommandButton.
  ctlCommand.Move (ctlDynamic.Left + ctlDynamic.Width + 50), _
    ctlText.Height + 50, 1500, 500
  ' Set the caption
  ctlCommand.Caption = "Click Me"
  ' Make it visible
  ctlCommand.Visible = True
End Sub

Respect ameba, if that works, I will take the code although.
I agree with db_tiger! You cant load a control without an array...

- Vidar Braut Haarr
  AmigoSoft Productions
Why not just load the control with the .Enabled and .Visible properties set to "False". Then, if ya need 'em, set those properties to "True", and there they are! Seems to take a lot less code......

kfrick, I think you should read the question before answering...

- Vidar Braut Haarr
  AmigoSoft Productions
vbkannAuthor Commented:
ameba...do you know anyway of me doing this in visual basic 5 sp3? as i dont have vb6.

Create Exception Handler
It raises an error so that it can
be trapped with an ON ERROR statement
in the procedure that caused the exception.

You will be able to continue with your program. This should also work for missing OCX-es.

This has been described in VBPJ May 99, but I don't have that article at a moment.
http://www.vbpj.com/ (requires premier membership at http://www.windx.com/)
I never got the exception handler from the may VBPJ to work.  I dled the source and it still does not work.  Did you have to change the code to get it to work
I didn't try - I just saw this.
This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects that and the expert will now receive these points, all verified.

Please click on your Member Profile, select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.
Moderator @ Experts Exchange
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.