Loading objects at runtime is not used much in VB(5/6). In a lot of other programming languages it is the only way to do it. But in VB most users like just to drop the desired object on the form, and costumize them in the IDE.
Some times it can be better to do this at run time. You can save some memory by doing this. Or you can save a lot of work if you want many many objects of the same type at a form, by loading them as a control array. One thing to think about though is that loading a lot of objects at run time can take a lot of time. So you have to carefully think about when to load them and when to show them. But now lets get to work. I am going to show you 3 ways to do this. There is more ways to do it. But I don't think you need more then maximum 3 right now.
The first way I will show you is just loading one single object. You can write event code for them at design time if you want, but the object can't use it before it has been made at run time. In this example I will make the object in the form load event. So when the form appears on the screen it will show right away. You can of course change this to any prefered event.
First you have to make a pointer to the kind of object you want to make. You do this by writing
You declear it more or less as a normal variable using Dim. The data type is CommandButton. The WitEvents keyword tells the app that you want to make it possible to write event code for the command button you are writing. But this pointer is not pointing to anything. It can point to a command button, but we have no command button yet. But we will make that soon. But first we will will write an Sub for the commandbutton we have made. Nothing new here. The Sub looks like this:
It looks just like a normal Sub you are writing. It is triggered when the click event of the command button is made. And it makes a message box appear with "hi" as the text.
No lets make the command button. As I said earlier I will write it in the forms load event. First we will make the command button like this:
Now we are setting the pointer that we made earlier to point to a new command button that we are making. We do that my setting it = Me.Controls.Add("VB.CommandButton", "newCommandButton" ). Me is the an "acronym" to the form you are using. If the form you are using is called Form1, you could as well write
Controls has a function for making new objects. It takes two parameteters. The first one takes the type of object you want to make. Here it is VB.CommandButton. VB is an object in Visual Basic, and that has a lot of functions and properties. And here we are telling the Add function that we want a command button object. The next parameter is the new name of the object. I have called it newCommandButton, but you can call it what ever you want.
Now we have made the object. But we have not told the app how it looks like. Lets do that now.
Here I am changing all the properties in the With block, but you can of course change all of them one by one if you like that. But the importent thing to notice here is that you have to set the Visible property to true to make it appear on the screen.
Now you have made the object and you are done. At least nearly there. You can do what ever you want with it, but you have a possible memory leak in the app. You don't need the pointer any more. So you should set that one to nothing. You do that like this:
Now you are finished. But you can't use the pointer to change the properties anymore like we did 2 minutes ago. But you can still change properties by using the new name you made for it. Like this:
But you can't use the Subs you have made for the command button after you did set the pointer to nothing. So don't do that before you don't need the pointer anymore. So lets wrap this up with the final code you need. And remember you don't need any controls on the form at design time to make this code work.
Some times it can be better to do this at run time. You can save some memory by doing this. Or you can save a lot of work if you want many many objects of the same type at a form, by loading them as a control array. One thing to think about though is that loading a lot of objects at run time can take a lot of time. So you have to carefully think about when to load them and when to show them. But now lets get to work. I am going to show you 3 ways to do this. There is more ways to do it. But I don't think you need more then maximum 3 right now.
The first way I will show you is just loading one single object. You can write event code for them at design time if you want, but the object can't use it before it has been made at run time. In this example I will make the object in the form load event. So when the form appears on the screen it will show right away. You can of course change this to any prefered event.
First you have to make a pointer to the kind of object you want to make. You do this by writing
visual basic code:
Dim WithEvents cmdButton As CommandButton
You declear it more or less as a normal variable using Dim. The data type is CommandButton. The WitEvents keyword tells the app that you want to make it possible to write event code for the command button you are writing. But this pointer is not pointing to anything. It can point to a command button, but we have no command button yet. But we will make that soon. But first we will will write an Sub for the commandbutton we have made. Nothing new here. The Sub looks like this:
visual basic code:
Private Sub cmdButton_Click() MsgBox "hi" End Sub
It looks just like a normal Sub you are writing. It is triggered when the click event of the command button is made. And it makes a message box appear with "hi" as the text.
No lets make the command button. As I said earlier I will write it in the forms load event. First we will make the command button like this:
visual basic code:
Set cmdButton = Me.Controls.Add("VB.CommandButton", "newCommandButton" )
Now we are setting the pointer that we made earlier to point to a new command button that we are making. We do that my setting it = Me.Controls.Add("VB.CommandButton", "newCommandButton" ). Me is the an "acronym" to the form you are using. If the form you are using is called Form1, you could as well write
visual basic code:
Set cmdButton = Form1.Controls.Add("VB.CommandButton", "newCommandButton" )
Controls has a function for making new objects. It takes two parameteters. The first one takes the type of object you want to make. Here it is VB.CommandButton. VB is an object in Visual Basic, and that has a lot of functions and properties. And here we are telling the Add function that we want a command button object. The next parameter is the new name of the object. I have called it newCommandButton, but you can call it what ever you want.
Now we have made the object. But we have not told the app how it looks like. Lets do that now.
visual basic code:
With cmdButton .Left = 1000 .Top = 1000 .Width = 2000 .Height = 500 .Caption = "Hello" .Visible = True End With
Here I am changing all the properties in the With block, but you can of course change all of them one by one if you like that. But the importent thing to notice here is that you have to set the Visible property to true to make it appear on the screen.
Now you have made the object and you are done. At least nearly there. You can do what ever you want with it, but you have a possible memory leak in the app. You don't need the pointer any more. So you should set that one to nothing. You do that like this:
visual basic code:
Set cmbButton = Nothing
Now you are finished. But you can't use the pointer to change the properties anymore like we did 2 minutes ago. But you can still change properties by using the new name you made for it. Like this:
visual basic code:
Controls("newCommandButton" ).Visible = False
But you can't use the Subs you have made for the command button after you did set the pointer to nothing. So don't do that before you don't need the pointer anymore. So lets wrap this up with the final code you need. And remember you don't need any controls on the form at design time to make this code work.
visual basic code:
Option Explicit Dim WithEvents cmdButton As CommandButton Private Sub cmdButton_Click() MsgBox "hi" End Sub Private Sub Form_Load() Set cmdButton = Form1.Controls.Add("VB.CommandButton", "newCommandButton" ) With cmbButton .Left = 1000 .Top = 1000 .Width = 2000 .Height = 500 .Caption = "Hello" .Visible = True End With End Sub Private Sub Form_Unload(Cancel As Integer) Set cmdButton = Nothing End Sub