DynamicControl

From HashVB
Jump to: navigation, search

The following code will dynamically create a textbox and a command button.

Option Explicit

Dim WithEvents txtBox As VB.TextBox
Dim WithEvents cmdBtn As VB.CommandButton

Private Sub cmdBtn_Click()
    MsgBox txtBox
End Sub

Private Sub Form_Load()
' create textbox control
    Set txtBox = Controls.Add("VB.TextBox", "txt")
' use textbox control
    With txtBox
        .Text = "hello"
        .Width = 1600
        .Height = 300
        .Visible = True
    End With
    
' create commandbutton control
    Set cmdBtn = Controls.Add("VB.CommandButton", "cmdBtn")
' use picturebox control
    With cmdBtn
        .Width = 1600
        .Height = 300
        .Left = txtBox.Width + 100
        .Caption = "Dynamic"
        .Visible = True
    End With
End Sub