Link to home
Start Free TrialLog in
Avatar of anandmehta
anandmehta

asked on

Control At Runtime.

At the Design time i have blank form.
means there is no control on it.
Now at the event of form load new control like text box,command button sould be placed.
i am not talking about control arrays.
(Control Creattion at Runtime).

Delphi supports this thing.
how to do this in VB?
Avatar of l99057j
l99057j

The only way I am aware of being able to dynamically create controls IS through the use of a control array.  You can create one control at design time, and then make as many as you need at runtime.

ASKER CERTIFIED SOLUTION
Avatar of robbert
robbert

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And why NOT create at design time?

* To save memory?  After you add it, it takes up the same memory whether added at design time or run-time.

* To hide from a user until needed?  Just make it invisible until needed.

* To allow the information to be read from a database?  OK, but this would be a pretty unusual case.

Then again, you can dynamically create in VBScript (CreatObject) but I've never tried it in VB.
And why NOT create at design time?

* To save memory?  After you add it, it takes up the same memory whether added at design time or run-time.

* To hide from a user until needed?  Just make it invisible until needed.

* To allow the information to be read from a database?  OK, but this would be a pretty unusual case.

Then again, you can dynamically create in VBScript (CreateObject) but I've never tried it in VB.
You need to delcare a variable to hold the reference to the control. Use the add method to create the instance of the control. Set the properties of the control to position it on the form and make it visible to you.

Try something like this...

   Private Sub Command1_Click()
   Dim otxtnew As TextBox

   Set otxtnew = Controls.Add("VB.Textbox", "otxtNew")
   With otxtnew
     .Visible = True
     .Text = "New Control"
     .Width = Me.TextWidth(.Text) + 200
     .Height = Me.TextHeight(.Text) + 50
     .Top = 100
     .Left = 300
   End With
   End Sub
Ruchi is correct, but I think you might want to add where you want to place the controls, so the line should read

Set otxtnew = Controls.Add("VB.Textbox", "otxtNew", FormName)

' or if creating from a form module.
Set otxtnew = Controls.Add("VB.Textbox", "otxtNew", Me)




Hi all,
  i have some question, when u using
Set otxtnew = Controls.Add("VB.Textbox", "otxtNew")
to create object in the form , but how u control the object, for example u , u create is a commandButton , not the extbox, so how u guys to do the click function ?

best regards
IAN
Anyone got the answer to ianyian last question? How do you handle the click event of the newly added button?

Thanks for all your help!!!
Try this as a test for what you want to do.

First place a textbox on the form and set its index = 1
Then place a button on the form with this code.

Private Sub Command1_Click()
Load Text1(2)
Text1(2).Visible = True
Text1(2).Top = 1000
LText1(2).Left = 1000
End Sub

Place this code in the form code.

Private Sub Text1_Click(Index As Integer)
Select Case Index
Case 1
MsgBox "TextBox 1"
Case 2
MsgBox "TextBox 2"
End Select
End Sub

The only let down is you must have the control "Index 1" present first of all.