PDA

View Full Version : [SOLVED] VBA - Create button and his event in a classe



edweis
06-10-2015, 01:09 AM
Hello,
Here what i want to do :
I have an userform with juste a frame "Frame1" and a button "Go" outside of it. When i click on this button, it would create a red button inside Frame1 (the more i click on "Go", the more red button comes) and whenever i click on a red button, i would have a MsgBox : "Hey !"

Here is my code :
-------------In the user form : ---------


Public a As New gestEvent
Private i As Integer


Private Sub Go_Click()
Set a = New gestEvent
Call a.init(Me.Frame1, i)
i = i + 1
End Sub


Private Sub UserForm_Initialize()
i = 0
End Sub



In the class "gestEvent" : ----------



Option Explicit
' Declare object variable as CommandButton.
Private WithEvents cmdObject As CommandButton

Public Sub init(f As frame, i As Integer)
Set cmdObject = f.Controls.Add("Forms.CommandButton.1", "cmdOne" & i)
cmdObject.Visible = True
cmdObject.BackColor = &HFF&
cmdObject.Top = 20 * i
cmdObject.Caption = "Dynamic CommandButton"
End Sub

Private Sub cmdObject_Click()
MsgBox "This is a dynamically added control"
End Sub


My Probleme is that only the last button created has the animation.
do you have any tips ?

Thank you

edweis
06-10-2015, 05:29 AM
I've posted it on an other forum and found a solution
mrexcel.com/forum/excel-questions/860454-visual-basic-applications-create-button-his-event-classe.html