Click to See Complete Forum and Search --> : Run Time Control Creatoin


Xemion
October 15th, 1999, 06:13 PM
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

Reid Robbins
October 15th, 1999, 07:35 PM
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

Xemion
October 15th, 1999, 09:07 PM
Thanks! I kept searching the help files but just couldn't find how to do it!

Xemion

Ravi Kiran
October 16th, 1999, 02:50 AM
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

Xemion
October 16th, 1999, 10:23 AM
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

Ravi Kiran
October 19th, 1999, 07:01 AM
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

Xemion
October 19th, 1999, 09:25 AM
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

Harry Gilbert
October 19th, 1999, 09:54 AM
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.

Xemion
October 21st, 1999, 08:49 AM
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

Harry Gilbert
October 21st, 1999, 09:12 AM
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.