Solved

Control At Runtime.

Posted on 2001-06-04
9
363 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now