VB icon

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

Email
Submitted on: 1/8/2015 2:29:00 PM
By: Jay Kreusch (from psc cd)  
Level: Beginner
User Rating: By 32 Users
Compatibility: VB 5.0, VB 6.0
Views: 939
 
     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.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Add ANY control at run-time (NOT the Load Statement!)
' Description: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.
' By: Jay Kreusch (from psc cd)
'**************************************

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


Other 5 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.