Results 1 to 9 of 9

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
    Location
    @ Opera Software
    Posts
    10,190

    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()
    2.  
    3.     Dim i As Integer
    4.  
    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
    16.  
    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 03:06 AM.

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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.

    Code:
        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
        Next

  3. #3
    New Member
    Join Date
    Oct 2015
    Posts
    11

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

    Dear Friend

    Thanks for your help. In your second example where you have used the arrayed control, how do we write events control array? What is the necessity of loading so many command button unless i can define the actions for each button. I have used for second example extensively in one of my form. But stuck up now. Pl advice me how to write events for control array using run time array control method (controls.add method) . Thanks

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