Capture events from runtime controls
Hello all,
can anyone help me by telling me how to capture the click events from some runtime command buttons that are in a form and which i don't know their name!
:confused: :confused: :confused: :confused: :confused: :confused: :confused: :confused: :confused: :confused: :confused: :confused:
Re: Capture events from runtime controls
Quote:
Originally Posted by DarkX_Greece
Hello all,
can anyone help me by telling me how to capture the click events from some runtime command buttons that are in a form and which i don't know their name!
:confused: :confused: :confused: :confused: :confused: :confused: :confused: :confused: :confused: :confused: :confused: :confused:
Do you mean that you add the controls to your form at runtime? If this is the case, what method are you using to do this? :)
Re: Capture events from runtime controls
If your adding them at runtime you need to declare a WithEvents for it in
the general declarations section.
If your loading an array of controls based off of an initial one added in design
time then you would use that objects event and check the index of the
control that is received in the procedures Index parameter.
Re: Capture events from runtime controls
Here is an example on how i create the runtime controls in a form! (command buttons)
VB Code:
Public Enum XControls
X_Command_Button = 0
X_Label = 1
X_TextBox = 2
X_Image = 3
X_ComboBox = 4
X_OptionButton = 5
X_CheckBox = 6
End Enum
Public Enum X_FontCharacteristics
xTrue = True
xFalse = False
End Enum
Public Enum X_FontSizes
Size_9 = 9
Size_10 = 10
Size_11 = 11
Size_12 = 12
Size_13 = 13
Size_14 = 14
Size_15 = 15
Size_16 = 16
Size_17 = 17
Size_18 = 18
Size_19 = 19
Size_20 = 20
Size_21 = 21
Size_22 = 22
Size_23 = 23
Size_24 = 24
Size_25 = 25
Size_26 = 26
Size_27 = 27
Size_28 = 28
Size_29 = 29
Size_30 = 30
End Enum
Public Enum X_Colors
Light_Red = &H8080FF
Red = &HFF&
Dark_Red = &HC0&
Blue = &HFF0000
Light_Green = &H80FF80
Green = &HFF00&
Dark_Green = &H8000&
Light_Blue = &HFFFF00
Dark_Blue = &HC00000
Orange = &H80FF&
Light_Orange = &H80C0FF
Dark_Orange = &H40C0&
Yellow = &HFFFF&
Black = &H0&
White = &HFFFFFF
Light_Grey = &HE0E0E0
Grey = &HC0C0C0
Dark_Grey = &H808080
Blue_Black = &H800000
Grey_Black = &H404040
Pink = &HFF80FF
Light_Pink = &HFFC0FF
Dark_Pink = &HFF00FF
End Enum
Private WithEvents CMD As CommandButton
Public Sub CreateControl(ControlType As XControls, Caption_Text As String, Height As Integer, Width As Integer, Top As Integer, Left As Integer, Optional Action As String, Optional Backcolor As X_Colors, Optional Forecolor As X_Colors, Optional FontName As String, Optional FontSize As X_FontSizes, Optional FontBold As X_FontCharacteristics, Optional FontItalic As X_FontCharacteristics, Optional FontStrikeThrough As X_FontCharacteristics)
Static countCMD As Integer
Select Case ControlType
'Check if it is Command Button
Case Is = 0
Set CMD = Me.Controls.Add("VB.CommandButton", "CMD" & countCMD + 1, Me)
'Change the value of the static variable for creating more controls
countCMD = countCMD + 1
With CMD
.Visible = True
'Check if there is fontname entered
If FontName <> "" Then
.Font = FontName
End If
'Check if there is fontsize entered
If FontSize <> 0 Then
.FontSize = FontSize
End If
'Check if there is fontbold entered
If FontBold <> 0 Then
.FontBold = FontBold
End If
'Check if there is fontitalic entered
If FontItalic <> 0 Then
.FontItalic = FontItalic
End If
'Check if there is fontStriked entered
If FontStrikeThrough <> 0 Then
.FontStrikethru = FontStrikeThrough
End If
.Width = Width
.Height = Height
.Left = Left
.Top = Top
.Caption = Caption_Text
End With
End Select
End Sub
I use this code to create a command button from my activex control, in the activex control!
My question is:
How can i know the exact name of the button which is clicked!
I know that the name of the command button should be "CMD" & countCMD + 1
but i must know the exact name when the button is clicked!
Hope you can help me! :( :(
I think there is a way to capture the control's name when i clicked by API....
Re: Capture events from runtime controls
One problem with the code the way you've presented it here is that you lose your reference to a command button once you've added a second one since you are assigning them all to the global CMD variable. I don't understand why you do this???
Re: Capture events from runtime controls
How can i assign them in seperate variables?
Is there any chance to do it without using table variable?
Re: Capture events from runtime controls
Quote:
Originally Posted by moeur
One problem with the code the way you've presented it here is that you lose your reference to a command button once you've added a second one since you are assigning them all to the global CMD variable. I don't understand why you do this???
But you can't have an array... :ehh:
1 Attachment(s)
Re: Capture events from runtime controls
Here is an example of adding controls at runtime using the second suggestion of RobDog888 :)
Re: Capture events from runtime controls
The code works and helped me a lot! Thank you Manavo11!
Re: Capture events from runtime controls
Quote:
Originally Posted by DarkX_Greece
The code works and helped me a lot! Thank you Manavo11!
You're welcome :)