?
Solved

Control At Runtime.

Posted on 2001-06-04
9
Medium Priority
?
370 Views
Last Modified: 2013-11-26
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?
0
Comment
Question by:anandmehta
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 2

Expert Comment

by:l99057j
ID: 6153357
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.

0
 
LVL 15

Accepted Solution

by:
robbert earned 300 total points
ID: 6153413
It's a little tricky but:

HOWTO: Dynamically Add Controls to a Form with Visual Basic 6.0
http://support.microsoft.com/support/kb/articles/Q190/6/70.ASP
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6153497
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:rspahitz
ID: 6153498
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.
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 6153637
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
0
 
LVL 4

Expert Comment

by:RichW
ID: 6153816
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)




0
 

Expert Comment

by:ianyian
ID: 7582065
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
0
 

Expert Comment

by:okwong
ID: 8095444
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!!!
0
 

Expert Comment

by:MDockery
ID: 8334280
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.

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month14 days, 8 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question