Creating a control in code


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Creating a control in code

  1. #1
    Join Date
    Oct 2004
    Posts
    57

    Creating a control in code

    Hello, in a program I am making I need to create a control, more specifically the control shape, during runtime. The Dim statement, Dim Rectangle as Shape, will work, but when I try to do, Set Rectangle = new Shape, i get an error stating the variable isn't defined, and Shape is highlighted.
    I've tried searching google, and the boards here for the answer, but I cannot find the answer. Any help would be appreciated, thanks.

  2. #2
    Join Date
    Nov 2003
    Location
    Sweden
    Posts
    74
    Hi,
    The easiest way is to put an invisible 'seed' of the Shape at the form and set it's visibilty to false. Then make an array of it by stating Index = 0. Now things become much more convenient.

    1) Create a new project and place a button (Command1) in the corner of Form1.
    2) Put a shape (Shape1) somewhere and set it's visibilty to False as well as Index = 0
    3) Copy and paste the code below
    4) Run it, and click the button.

    Code:
    Private Sub Command1_Click()
    
        'Creates a new shape within the Shape1() array...
        Load Shape1(1)
        
        'The new one inherits the seed's properties so adjust
       'them the way you want...
        With Shape1(1)
            .Top = 100
            .Left = 100
            '...and finally make it visible!
            .Visible = True
        End With
        
    End Sub
    You got the picture? Just keep track of the Index and you may create any number of Shapes (or any other control as well) and manipulate each one's properties individually.

    Bernie

  3. #3
    Join Date
    Oct 2004
    Posts
    57

    Thanks a lot

    It works perfect! Thanks a bunch

  4. #4
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    You can also do this, without creating a seed control at design time:
    Code:
    Set Rectangle = Controls.Add("VB.Shape", "Rectangle", Me)
    With Rectangle
        .Top = 100
        .Left = 100
        .Visible = True
    End With
    For more information, see http://www.johnsmiley.com/cis18.notf.../smiley007.htm
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  5. #5
    Join Date
    Nov 2003
    Location
    Sweden
    Posts
    74
    Your'e welcome!

    I wouldn't post a snippet that wasn't tested first. By the way, somewhere one the Visual Studio disks there is a sample showing how to create a control completely from scratch. I tried that one, but that was much more tricky. Using an invisible seed is more convenient since you already have every event and method there to code. Just the Index needs to be tracked. For some reason you can't use Ubound()/LBound() on control arrays. A 'parallell' integer array that is updated simultaniously is a good idea, especially if the app creates and/or deletes controls more or less randomly. Trying to Load an index already there will give Error 360.

    By the way, if you want to remove one of the shapes, simply use Unload Shape1(IndexToRemove).

    A few years ago I made an advanced planning table for an enterprise maintenance system. Workin orders where to be 'dragged and dropped' and movable/stretchable among time and resources and the underlaying DB updated accordingly. I used labels for that, since they have all mouse and click events.

    It's my pleasure to be of any help!

    Bernie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center