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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating Control during runtime

Status
Not open for further replies.

Haheehi

Programmer
Sep 4, 2000
28
0
0
SG
How do i create a command button dunring runtime

I tried:
dim myCmd as commandbutton
set myCmd = new commandbutton



But i get an error. [sig][/sig]
 
to create a control array called cmdBtn (you have to have one control button on the form at design time)
do this:

Dim j As Integer

For j = 1 to 9
Load cmdBtn(j)
Next j

I hope this helps.

Billie
 
I forgot to tell you that you have to make space when you load them.

Dim leftPos As Integer, j As Integer
Dim buttonWidth As Integer

'Get the size and position of the first command button
leftPos = cmdBtn(0).Left 'this is the one you created at
' design time
buttonWidth = cmdBtn(0).Width

'Position the other buttons and make them visible. You need
'this because when they are loaded at run time their visible
'property is set to false

For j = 1 To 9
Load cmdBtn(j)
leftPos = leftPos + buttonWidth + 30
cmdBtn(j).Left = leftPos
cmdBtn(j).Visible = True
Next j

Good luck

 
What if I wanted to load a label instead. I tried this, but it didn't work for labels.
 
or you could try something like...

Code:
Option Explicit

Private WithEvents moMyLabel As Label

Private Sub Command1_Click()
  Set moMyLabel = Controls.Add("VB.Label", "moMyLabel")
  moMyLabel.AutoSize = True
  moMyLabel.Caption = "Click here!"
  moMyLabel.Move (ScaleWidth - moMyLabel.Width) / 2, 0
  moMyLabel.Visible = True
End Sub

Private Sub moMyLabel_Click()
  MsgBox "You clicked me!"
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top