-
I need to add events to a new object during runtime. This is what I have so far:
Code:
Dim Form_To_Create As Form
Form_to_Create.Caption = "Test"
Form_To_Create.Show
How would I add code to the Form_Click Event?
Also, how would you put an image over another image?
I tried a picturebox over another but you saw the square of the object over the form, but I would like to be able to see just the image in front. Thanks.
-
That won't work, to actually create the form you need to use New keyword and a class, in this case form1, which you would have to do in designtime
Code:
Dim Form_To_Create As Form
Set Form_To_Create = New Form1 '<---
Form_To_Create.Caption = "Test"
Form_To_Create.Show
On the other hand this form will be created and can stil be changed during runtime
-
But how can I add code to Form1_Click of the new Form after I create it?
-
Use the WithEvents statement.
-
-
This will add an event for a button created at runtime
Code:
Private WithEvents cmd As CommandButton
Private Sub Form_Load()
Set chkBox = Me.Controls.Add("VB.CommandButton", "Btn")
Me!Btn.Move 0, 0
Me!Btn.Caption = "Button1"
Me!Btn.Visible = True
End Sub
Private Sub cmd_Click()
MsgBox "You pressed the button"
End Sub
-
Please apply this...
How would you have a button, that when clicked, created a button that, when clicked, popped up a MsgBox that said "hello, you have clicked the second Command Button that was created during runtime by another Command button."?
-
Add the following to a Form with 1 CommandButton.
Code:
Private WithEvents cmd As CommandButton
Private Sub Command1_Click()
Set cmd = Me.Controls.Add("VB.CommandButton", "Btn")
Me!Btn.Move 0, 0
Me!Btn.Caption = "NewButton"
Me!Btn.Visible = True
End Sub
Private Sub cmd_Click()
MsgBox "hello, you have clicked the second Command Button that was created during runtime by another Command button"
End Sub
-
last question!
When I do the above it only works to create one. What would I do to create another after another button that say "hello, you have clicked the second Command Button that was created during runtime by another Command button" when you click the original Button? Now, it get an error saying there is already a Btn on the form.
-
Also, without arrays. In other words, have the function creat a button and add the name to a list box. You can click the original button again and again, each time adding a Btn1, Btn2 and so forth(remember, no arrays) and when you click the new buttons, they all do the same things (i.e. MsgBox "you clicked me")Anyone?