Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Control At Runtime.

Posted on 2001-06-04
9
Medium Priority
?
372 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

636 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