VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Dynamically Added Objects

by CJ Schaff (1 Submission)
Category: Miscellaneous
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (3 Votes)

This short, simple code allows the addition of objects into a container on another form (in this case a PicturBox). It can easily be modified to add the object to different locations by modifying the locations in the dynObject() function.

Inputs
Create 2 forms. Add a CommandButton to Form1, and add a PictureBox for Form2.
Code Returns
Pressing the first Command1 loads a label on Form2 in the picture box and a command button on Form1. Pressing the new command button will activate a message box.
Side Effects
Pressing Command1 again will cause an error which should be handled in an actual application.

Rate Dynamically Added Objects

Dim WithEvents dynbutton As VB.CommandButton
Dim WithEvents dynLabel As VB.Label
Private Sub Form_Load()
 Form2.Show
 Form2.Top = Form1.Top
 Form2.Left = Form1.Left + Form1.Width
End Sub
Private Sub Command1_Click()
 Call dynObjects
End Sub
   
Public Sub dynObjects()
 'Define label location and properties
   Set dynLabel = Form2.Controls.Add("VB.label", "dynLabel", Form2.Picture1)
    dynLabel.Caption = "Dynamically added label!"
    dynLabel.Visible = True
    dynLabel.BorderStyle = 1
   
 'Define CommandButton location and properties
   Set dynbutton = Form1.Controls.Add("VB.commandbutton", "dynButton", Form1)
    dynbutton.Caption = "Dynamic Button"
    dynbutton.Visible = True
    dynbutton.Width = 1275
    dynbutton.Font = "MS Sans Serif"
 End Sub
Private Sub dynButton_click()
 MsgBox ("You have pressed a dynamically added button")
End Sub

Download this snippet    Add to My Saved Code

Dynamically Added Objects Comments

No comments have been posted about Dynamically Added Objects. Why not be the first to post a comment about Dynamically Added Objects.

Post your comment

Subject:
Message:
0/1000 characters