Solved

Control At Runtime.

Posted on 2001-06-04
9
367 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
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 100 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
Add a task in Outlook from access 11 42
vbModal 12 57
RUNRMTCMD from AS/400 12 69
VBA: loop recent folder and copy txt file. 8 36
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

828 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