CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Oct 1999
    Posts
    15

    Run Time Control Creatoin

    How do I stick a control in at run time? Like for every time you click a button, a new text box appears? Can I have the new text box be in a control array? Are these stupid questions?

    Please Help!

    Xemion


  2. #2
    Join Date
    Oct 1999
    Location
    S.W. Wyoming
    Posts
    25

    Re: Run Time Control Creatoin

    To add a control at run time, you must have previously created a control array with at least one element. Then, at run time, use the "Load" statement to create a new element.
    To initially create the control array, draw one control (for example, your text box) on your form (it may be invisible, if this is your need). Then set its "Index" property to 0 (or, actually, ANY number from 0-32,767). This will create a control array. In code, you could then say


    Load MyTextBox(1)
    Load MyTextBox(2)




    Note that you could actually "Load" MyTextBox(100), even if you only had element 0 earlier. But you'd not be able to reference MyTextBox(99) or MyTextBox(101). You can only refernce those indices you have drawn or subsequently loaded. Each newly created control inherits the properties of the control originally drawn. You can later destroy all elements (save the one you drew on the form) with an Unload statement. Since a newly loaded control will very likely not have the correct properties, you might want to create the initial control as "Visible=False", load the new control (which is then also invisible), set all properties (position, for example) and only then make the new control visible.

    There ARE no stupid questions, except the ones you don't ask!


    Reid Allen Robbins
    Green River, WY 82935

  3. #3
    Join Date
    Oct 1999
    Posts
    15

    Re: Run Time Control Creatoin

    Thanks! I kept searching the help files but just couldn't find how to do it!

    Xemion


  4. #4
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    Re: Run Time Control Creatoin

    Reis's method is fine, for all VB 4/5/6.

    But in VB 6, Form.Controls supports a new method called Add, which lets you create totally new 'run-time' controls w.o having to create one in the design mode. Look for help on Controls.Add method.

    RK

  5. #5
    Join Date
    Oct 1999
    Posts
    15

    Re: Run Time Control Creatoin

    Ok, I'm using Reid's method. I couldn't get RK's to work (don't know why :-). It creates the new control, but it's at the bottom of all my other controls. How do I move it to do the top, or move the rest to the bottom?

    Thanks!

    Xemion


  6. #6
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    Re: Run Time Control Creatoin

    Obviously, when you create things at run time, many of its properties are not set, and you need to set them, like positions, visibility, value (if any) etc.

    RK

  7. #7
    Join Date
    Oct 1999
    Posts
    15

    Re: Run Time Control Creatoin

    Ok, but how do I move the object's order? Like I have a picture box or something covering the whole screen and I create a new control, it's behind toe picture box so you can see it. How do I put it in front of the picture box so it's visible?

    Thanks!

    Xemion


  8. #8
    Join Date
    Jul 1999
    Posts
    11

    Re: Run Time Control Creatoin

    Creating controls at runtime is pretty easy but not well documented. Here is an example: On your form, drop a textbox and give it an index value of 0 (zero). Set up any properties, color, font etc. Now in your code, do this

    private Sub Form_Load()
    Load Text1(1) ' load a new one, any index will do (except 0)
    With Text1(1)
    .Top = Text1(0).Top + Text1(0).Height
    .Vorecolor = vbCyan
    .Visible = true ' controls are invisible when created
    End With
    End Sub



    Do get rid of them, use Unload.





  9. #9
    Join Date
    Oct 1999
    Posts
    15

    Re: Run Time Control Creatoin

    Ok, that works, but I have a problem.
    Say you make a big command button. Then make Text1(0) on top of the button. When you run the program, you can't see Text1(1). How can I make it come forward without makine command1 invisible?
    Thanks!

    Xemion


  10. #10
    Join Date
    Jul 1999
    Posts
    11

    Re: Run Time Control Creatoin

    Do the following:

    Text1(1).ZOrder



    This sets the 'zorder' (order of appearance) of the Text1(1) box to be on top of the window list. This only works with 'true' controls, not lightweight controls like Labels, Images, and Shapes (which are owner-drawn). That is, you cannot make a Label appear on top of a textbox, command button, etc. Look up ZOrder in your help for more info.



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

Featured