Search Tools Links Login

Add ANY control at run-time (NOT the Load Statement!)


Adds a control to your form at run-time. Does not use control arrays or the Load statement. Control does not even need to be referenced. NO API.

Original Author: Jay Kreusch

Code

Option Explicit
' If you are adding an ActiveX control at run-time that is
' not referenced in your project, you need to declare it
' as VBControlExtender.
Dim WithEvents ctlDynamic As VBControlExtender
Dim WithEvents ctlCommand As VB.CommandButton
Dim WithEvents ctlText As VB.TextBox
Private Sub ctlCommand_Click()
  'since we delcared withevents, we can use them
  ctlDynamic.object.Value = CDate(ctlText.Text)
End Sub
Private Sub ctlDynamic_ObjectEvent(Info As EventInfo)
  
  'This is sort of an 'all-in-one' event
  'so you have to check parameters and event name
  Dim p As EventParameter
  Debug.Print Info.Name
  
  For Each p In Info.EventParameters
    Debug.Print p.Name, p.Value
  Next
  Select Case Info.Name
    Case "NewMonth"
      ctlText.Text = ctlDynamic.object.Value
    Case "Click"
      MsgBox ctlDynamic.object.Value
  End Select
End Sub
Private Sub Form_Load()
  'If you get run-time error number 732.
  'Then the control isn't in the liscenses collection
  'Use this line with the ProgID you want
  'Licenses.Add [ProgID]
    
  ' Add a control and set the properties of the control
  Set ctlDynamic = Controls.Add("mscal.calendar", "calMain", Form1)
  With ctlDynamic
    .Move 1, 400, 4000, 3000
    .Visible = True
  End With
  
  ' add a textbox and set properties for the textbox
  Set ctlText = Controls.Add("VB.TextBox", "ctlText1", Form1)
  With ctlText
    .Move 1, 1, 3400, 100
    .Text = ctlDynamic.object.Value
    .Visible = True
  End With
  
  ' Add a CommandButton.
  Set ctlCommand = Controls.Add("VB.CommandButton", "ctlCommand1", Form1)
  With ctlCommand
    .Move 3450, 1, 450, 300
    .Caption = "Go!"
    .Visible = True
  End With
End Sub

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 88 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.