Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Capture click Event on Dynamically Created control

Status
Not open for further replies.

jeniexcel

Programmer
Aug 1, 2010
3
BR
Hi,

I am new programmer in excel/vba and like capture a event click on control created dinamically in userform a runtime execution, follow the code part writed:

Private Sub Image1_Click()
Set mycmd = Frame1.Controls.Add("Forms.image.1", "nome" & i, True)
mycmd.Height = 12
mycmd.Width = 12
mycmd.BackColor = &H4080&
With ThisWorkbook.VBProject.VBComponents("Userform1").CodeModule
linha = .countoflines
MyScript(0) = "Private Sub nome" & i & "_Click()"
MyScript(1) = "set mycmd= frame1.controls(nome" & i & ")"
MyScript(2) = "label2.caption=" & i
.InsertLines linha + 3, MyScript(0)
.InsertLines linha + 5, MyScript(1)
.InsertLines linha + 6, MyScript(2)
.InsertLines linha + 7, "End Sub"
End With
i = i + 1
End Sub

 
You need a variable declared with WithEvents, predefined event code and object assigned to declared variable (all in class/object module).
You will find available events for declared variable in the right drop-dovn, in standard way. In your case:
Code:
' extra declaration for run-time control 
Private WithEvents mycmd As Image

Private Sub Image1_Click()
Set mycmd = Frame1.Controls.Add("Forms.image.1", "nome" & i, True)
' etc.
End Sub

Private Sub mycmd_Click()
' extra code for run-time image
' header created after selection of object and event
MsgBox "fire!"
End Sub





combo
 
Or......

depending on what you are trying to do, have a boiler-plate text file of the procedure, determine the name you want to use for the control, change the name in text file to that name, save it as a .bas file and then inport it into the VBAProject.

OR.......change the name as put it as a string variable, and import it (AddFromString). As it seems you are using quite small amount of code, it would be easy to bring in the boiler-plate text, use Replace for your "nome" variable, and then use AddfromString to write it as a procedure.

As a personal opinion, after years of VBA I have never, ever, seen a truly valid use of a run-time creation of a control (and attendant _Click event code). Oh, I can certainly see the need for making it look like there is a new control, but I have always been able to handle this using .Visible (and/or resizing the userform).

So I am curious. If possible, could you explain what it is you are doing?

Gerry
 
combo,

thanks, this code is very important when we have one control in form, but i'm working with n controls, where n is defined by user and stay impossible declare n private with events object.
 
fumei,

i am developing one application with various controls in form, with choice the user new control is add, but i need change the refer of the mycmd for the user can operate any of controls in the form, and i'm tryng do it with the event click on control created.
 
Having one (separate) class module handling image's events there is no problem to declare an array and assign image to each item. You only need to change WithEvents to Public.
Maybe this faq707-4976 will be useful too.

combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top