Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding a control to a form at run time 1

Status
Not open for further replies.

SpiritOfLennon

IS-IT--Management
Oct 2, 2001
250
GB
Hi,
Can anyone give me any suggestions/advice as to how to add a new control to a form at runtime. Basically I have a button on a form. When clicked it brings up a sub form where the user can specify values. Onece they accept this values it is to use the values to add a new control to the original form. Any help ass to which commands to look at woould be appreciated.

SOL
I'm only guessing but my guess work generally works for me.
 
Try this code in a new project!!!!


The withevents keyword is used in this code, since it is necessary to be able to trap any events the user generates from these controls.
Add a CommandButton to a form and call it Command1, then paste this code to the form and try running it. When the command button is pressed a new CommandButton and Textbox should be created at runtime and displayed.
Dim WithEvents cmdNewButton As VB.CommandButton
Dim WithEvents txtNewTextbox As VB.TextBox

Private Sub Command1_Click()
`Add a command button
Set cmdNewButton = Controls.Add("VB.CommandButton", "cmdNewButton", Me)
cmdNewButton.Move 0, 0
cmdNewButton.Visible = True

`Add a Textbox
Set txtNewTextbox = Controls.Add("VB.TextBox", "txtNewTextBox", Me)
txtNewTextbox.Move 500, 500, 1000, 1000
txtNewTextbox.Visible = True
End Sub



It works perfectly!!!!

scsii
 
If it's always the same control, design it onto the form and set it's visible property to False. Your second form can now handle it, and set any other properties at the same time:

Form1.cmdNew.Visible = True
Form1.cmdNew.Caption = "New Control"


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Thanks Scsii, your speed and your code are much appreciated. Now I should be able to figure out how to repetitively call the routine.

SOL
I'm only guessing but my guess work generally works for me.
 
Does anyone know if it's possible to create an object at runtime inside a frame or other container?

SOL
I'm only guessing but my guess work generally works for me.
 
Just change the keyword "Me" to the name of the container
 
Wow, thanks CCLINT.
I'm learning all kinds of stuff today. Sorry for asking dumb questions but I have to learn lots of really hard VB stuff really fast. Maybe I should buy a book.

SOL
I'm only guessing but my guess work generally works for me.
 
Hello again everybody,
Does anybody know how to set the container to a tab on an sstab. I can't seem to figure it out.


SOL
I'm only guessing but my guess work generally works for me.
 
You have to set the tab of the SSTab control to the one you want the new control to go on.

SSTab.Tab = X ' Set the tab #

Then set the container of the control to the SSTab.

Set MyControl.Container = SSTab
MyControl.Move 100, 200, 300, 400 ' Set the position of the control inside the SSTab. Your values will be different.
MyControl.Visible = True ' This may or may not be necessary.

HTH,

Robert
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top