Results 1 to 10 of 10

Thread: Capture events from runtime controls

  1. #1

    Thread Starter
    Hyperactive Member DarkX_Greece's Avatar
    Join Date
    Jan 2004
    Location
    Athens (Greece)
    Posts
    315

    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!

    Short CV:
    1. Visual Basic 6 Programmer
    2. Web Expert


    Botonakis Web Services

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    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!

    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?


    Has someone helped you? Then you can Rate their helpful post.

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Hyperactive Member DarkX_Greece's Avatar
    Join Date
    Jan 2004
    Location
    Athens (Greece)
    Posts
    315

    Re: Capture events from runtime controls

    Here is an example on how i create the runtime controls in a form! (command buttons)

    VB Code:
    1. Public Enum XControls
    2.     X_Command_Button = 0
    3.     X_Label = 1
    4.     X_TextBox = 2
    5.     X_Image = 3
    6.     X_ComboBox = 4
    7.     X_OptionButton = 5
    8.     X_CheckBox = 6
    9. End Enum
    10. Public Enum X_FontCharacteristics
    11.     xTrue = True
    12.     xFalse = False
    13. End Enum
    14. Public Enum X_FontSizes
    15.     Size_9 = 9
    16.     Size_10 = 10
    17.     Size_11 = 11
    18.     Size_12 = 12
    19.     Size_13 = 13
    20.     Size_14 = 14
    21.     Size_15 = 15
    22.     Size_16 = 16
    23.     Size_17 = 17
    24.     Size_18 = 18
    25.     Size_19 = 19
    26.     Size_20 = 20
    27.     Size_21 = 21
    28.     Size_22 = 22
    29.     Size_23 = 23
    30.     Size_24 = 24
    31.     Size_25 = 25
    32.     Size_26 = 26
    33.     Size_27 = 27
    34.     Size_28 = 28
    35.     Size_29 = 29
    36.     Size_30 = 30
    37. End Enum
    38. Public Enum X_Colors
    39.     Light_Red = &H8080FF
    40.     Red = &HFF&
    41.     Dark_Red = &HC0&
    42.     Blue = &HFF0000
    43.     Light_Green = &H80FF80
    44.     Green = &HFF00&
    45.     Dark_Green = &H8000&
    46.     Light_Blue = &HFFFF00
    47.     Dark_Blue = &HC00000
    48.     Orange = &H80FF&
    49.     Light_Orange = &H80C0FF
    50.     Dark_Orange = &H40C0&
    51.     Yellow = &HFFFF&
    52.     Black = &H0&
    53.     White = &HFFFFFF
    54.     Light_Grey = &HE0E0E0
    55.     Grey = &HC0C0C0
    56.     Dark_Grey = &H808080
    57.     Blue_Black = &H800000
    58.     Grey_Black = &H404040
    59.     Pink = &HFF80FF
    60.     Light_Pink = &HFFC0FF
    61.     Dark_Pink = &HFF00FF
    62. End Enum
    63. Private WithEvents CMD As CommandButton
    64. 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)
    65. Static countCMD As Integer
    66.  
    67. Select Case ControlType
    68. 'Check if it is Command Button
    69. Case Is = 0
    70.     Set CMD = Me.Controls.Add("VB.CommandButton", "CMD" & countCMD + 1, Me)
    71.     'Change the value of the static variable for creating more controls
    72.     countCMD = countCMD + 1
    73.     With CMD
    74.         .Visible = True
    75.         'Check if there is fontname entered
    76.         If FontName <> "" Then
    77.             .Font = FontName
    78.         End If
    79.         'Check if there is fontsize entered
    80.         If FontSize <> 0 Then
    81.             .FontSize = FontSize
    82.         End If
    83.         'Check if there is fontbold entered
    84.         If FontBold <> 0 Then
    85.             .FontBold = FontBold
    86.         End If
    87.         'Check if there is fontitalic entered
    88.         If FontItalic <> 0 Then
    89.             .FontItalic = FontItalic
    90.         End If
    91.         'Check if there is fontStriked entered
    92.         If FontStrikeThrough <> 0 Then
    93.             .FontStrikethru = FontStrikeThrough
    94.         End If
    95.         .Width = Width
    96.         .Height = Height
    97.         .Left = Left
    98.         .Top = Top
    99.         .Caption = Caption_Text
    100.     End With
    101. End Select
    102. 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....
    Short CV:
    1. Visual Basic 6 Programmer
    2. Web Expert


    Botonakis Web Services

  5. #5
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    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???

  6. #6

    Thread Starter
    Hyperactive Member DarkX_Greece's Avatar
    Join Date
    Jan 2004
    Location
    Athens (Greece)
    Posts
    315

    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?
    Short CV:
    1. Visual Basic 6 Programmer
    2. Web Expert


    Botonakis Web Services

  7. #7
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    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...


    Has someone helped you? Then you can Rate their helpful post.

  8. #8
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Capture events from runtime controls

    Here is an example of adding controls at runtime using the second suggestion of RobDog888
    Attached Files Attached Files


    Has someone helped you? Then you can Rate their helpful post.

  9. #9

    Thread Starter
    Hyperactive Member DarkX_Greece's Avatar
    Join Date
    Jan 2004
    Location
    Athens (Greece)
    Posts
    315

    Re: Capture events from runtime controls

    The code works and helped me a lot! Thank you Manavo11!
    Short CV:
    1. Visual Basic 6 Programmer
    2. Web Expert


    Botonakis Web Services

  10. #10
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    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


    Has someone helped you? Then you can Rate their helpful post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width