[VB] How to load a control at runtime.-VBForums
Results 1 to 4 of 4

Thread: [VB] How to load a control at runtime.

Hybrid View

  1. #1

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    @ Opera Software

    [VB] How to load a control at runtime.

    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

    VB Code:
    1. 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:

    VB Code:
    1. Private Sub cmdButton_Click()
    2.      MsgBox "hi"
    3. 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:

    VB Code:
    1. 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

    VB Code:
    1. 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.

    VB Code:
    1. With cmdButton
    2.         .Left = 1000
    3.         .Top = 1000
    4.         .Width = 2000
    5.         .Height = 500
    6.         .Caption = "Hello"
    7.         .Visible = True
    8. 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:

    VB Code:
    1. 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:

    VB Code:
    1. 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.

    VB Code:
    1. Option Explicit
    3. Dim WithEvents cmdButton As CommandButton
    5. Private Sub cmdButton_Click()
    6.      MsgBox "hi"
    7. End Sub
    10. Private Sub Form_Load()
    12.     Set cmdButton = Form1.Controls.Add("VB.CommandButton", "newCommandButton" )
    14.     With cmbButton
    15.         .Left = 1000
    16.         .Top = 1000
    17.         .Width = 2000
    18.         .Height = 500
    19.         .Caption = "Hello"
    20.         .Visible = True
    21.      End With
    23. End Sub
    25. Private Sub Form_Unload(Cancel As Integer)
    26.     Set cmdButton = Nothing
    27. End Sub

    Thats all.

    Last edited by NoteMe; May 31st, 2006 at 03:37 AM.

  2. #2

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    @ Opera Software

    Re: [VB] How to load a controll at runtime.

    The next example I am going to show you are not that diffrent. So lets go a bit faster thrue this time. I will now make an array of controls of the same type. The problem here is that you can't make event code for the object. The last tehnique after this one will shoe you how you can accomplishe that.
    So lets start. First we have to make an array of pointers that can point to the command buttons that we like. You do that nearly the same way you did las time:

    VB Code:
    1. Dim cmdButton(4) As CommandButton

    The only diffrence is that you this time made an array. (4) shows you that you now have made 5 pointers to the array. From 0 to 4. And the other thing is that we have omitted the WithEvents keyword. That is becuase we can't write event code for a controllaray that we have made at run time yet. But now it is time to make the objects. Lets make a simple loop to go through the pointers and make the objects.

    VB Code:
    1. Dim i As Integer
    3.     For i = 0 To 4
    4.         Set cmdNew(i) = Me.Controls.Add("VB.CommandButton", "cmdButton" & Me.Controls.Count)
    5.         With cmdNew(i)
    6.             .Left = 750 * i
    7.             .Top = 1000
    8.             .Width = 700
    9.             .Height = 500
    10.             .Caption = "Hello"
    11.             .Visible = True
    12.          End With
    13.     Next i

    This example is using a simple for loop so you don't have to write the code 5 times to make the objects. The only thing that is diffrent here is that we have added "& Me.Controls.Count" to the name parameter in the add funtion. We have done that just to make diffrent names of the commandbuttons that we have mede. The other thing that is changed is that I have changed the .Left property to 750 * i just to make sure that the object are not on top of each other, so you can see all of them. After this you just have to set all the pointer to Nothing like we did in the first example when you are finished wiht them. And there is nothing more to it then that. So here is the whole code.

    VB Code:
    1. Dim cmdButton(4) As CommandButton
    3. Private Sub Form_Load()
    5.     Dim i As Integer
    7.     For i = 0 To 4
    8.         Set cmdButton(i) = Me.Controls.Add("VB.CommandButton", "cmdButton" & Me.Controls.Count)
    9.         With cmdButton(i)
    10.             .Left = 750 * i
    11.             .Top = 1000
    12.             .Width = 700
    13.             .Height = 500
    14.             .Caption = "Hello"
    15.             .Visible = True
    16.         End With
    17.     Next i
    19. End Sub
    21. Private Sub Form_Unload(Cancel As Integer)
    23.     Dim i As Integer
    25.     For i = 0 To 4
    26.         Set cmdButton(i) = Nothing
    27.     Next i
    29. End Sub

    Last edited by NoteMe; May 31st, 2005 at 04:04 AM.

  3. #3

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    @ Opera Software

    Re: [VB] How to load a controll at runtime.

    Now for the last example you need to use the IDE a bit more. The simples way to accomplish this is to make a command button or what ever you want and drag it to the form like you normaly do. Change the name to what you want. Like I did: cmdButton. Then copy the command button and paste it to the form again. Then you will have a pop up message that asks you if you want to make a control array. Press yes. Then you have a control array called cmdButton of type command buttons. Now you can delete the second command buttons if you want. Becuase now we are going to load as many as we want using code. You do it like this:

    VB Code:
    1. Private Sub Form_Load()
    3.     Dim i As Integer
    5.     For i = 1 To 4
    6.         Load cmdButton(i)
    7.         With cmdButton(i)
    8.             .Left = 750 * i
    9.             .Top = 1000
    10.             .Width = 700
    11.             .Height = 500
    12.             .Caption = "Hello"
    13.             .Visible = True
    14.         End With
    15.     Next i
    17. End Sub

    And that is all you need. You don't have to make pointers, and you don't have to set any pointers to nothing. Pretty simple. Just remember that you have all ready loaded at least one object, so you can't load that one again. That is why my for loop goes from 1 and not 0. If I didn't delete the second command button that I made, then it had to go from 2 and so on. We are using the load function to load a new instance. Remember no paranteses around the object that you want to load. The rest of the code you have looked at all ready. This way you can make a lot of things easier. But remember that it takes a lot of CPU power to load the objects. So if you are making a progress bar or anything this way and you are loading the object while you are showing the progress a lot of the CPU power will go to make the objects. So it can be a better to make the objects earlier in the app, and then show them when you need it. But you will probably find out what is best for you when you are making the app. If you have questions about this tutorial please post a question in the forum, where there is probably more then just me that can answer.

    Last edited by NoteMe; May 31st, 2005 at 04:06 AM.

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    San Jose, CA

    Re: [VB] How to load a controll at runtime.

    In reference to the 3rd example where you create a control via the IDE, it's not necessary to create a second member (and then possibly delete it) in order to accomplish the creation. Instead all you need to do is to place one control on the form and change its Index to 0 and that will make it a control array.

    It should also be noted that while you can Unload any members of the control array that are created at run time you can't Unload the member(s) created in the IDE.

    Finally if you want to check the status of the members of a checkbox control array for example and the possibility exists that you might have Unloaded one of the middle members then do this.

        Dim cb As CheckBox
        For Each cb In Check1 ' The control array
            If cb.Value = vbChecked Then
                MsgBox "My Index is " & cb.Index
            End If

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


Click Here to Expand Forum to Full Width

Survey posted by VBForums.