在声明中加入: Private WithEvents NewButton As CommandButton ... 在事件中加入: Set NewButton = Controls.Add("VB.CommandButton", "cmdNew", Me) NewButton.Move Command1.Left + Command1.Width + 240, Command1.Top NewButton.Caption = "new button" NewButton.Visible = True 即可
Private WithEvents NewButton As CommandButton Private Sub Form_Load() Set NewButton = Controls.Add("VB.CommandButton", "cmd", Me) NewButton.Move 0, 0, 1000, 1000 NewButton.Caption = "button" NewButton.Visible = True End Sub Sub newButton_Click() MsgBox "OK" End Sub
如果直接加到窗体上可以用楼上几位的方法,如果是加到控件上,可以使用SetParent函数和Move函数来控制:'动态添加Text控件到SSTab各个页面上 Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long Dim myText() As VB.TextBoxPrivate Sub Command1_Click() ReDim myText(SSTab1.Tabs - 1) Dim i% For i = 0 To SSTab1.Tabs - 1 SSTab1.Tab = i Set myText(i) = Form1.Controls.Add("vb.TextBox", "txt" & i & "") myText(i).Visible = True myText(i).Text = "txt" & i SetParent myText(i).hWnd, SSTab1.hWnd myText(i).Move 350, 500, 1000, 330 Next SSTab1.Tab = 0 End Sub
Private WithEvents NewButton As CommandButton
...
在事件中加入:
Set NewButton = Controls.Add("VB.CommandButton", "cmdNew", Me)
NewButton.Move Command1.Left + Command1.Width + 240, Command1.Top
NewButton.Caption = "new button"
NewButton.Visible = True
即可
Private Sub Form_Load()
Set NewButton = Controls.Add("VB.CommandButton", "cmd", Me)
NewButton.Move 0, 0, 1000, 1000
NewButton.Caption = "button"
NewButton.Visible = True
End Sub
Sub newButton_Click()
MsgBox "OK"
End Sub
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Dim myText() As VB.TextBoxPrivate Sub Command1_Click()
ReDim myText(SSTab1.Tabs - 1)
Dim i%
For i = 0 To SSTab1.Tabs - 1
SSTab1.Tab = i
Set myText(i) = Form1.Controls.Add("vb.TextBox", "txt" & i & "")
myText(i).Visible = True
myText(i).Text = "txt" & i
SetParent myText(i).hWnd, SSTab1.hWnd
myText(i).Move 350, 500, 1000, 330
Next
SSTab1.Tab = 0
End Sub