Results 1 to 5 of 5

Thread: [RESOLVED] how to add handler to runtime created control

  1. #1

    Thread Starter
    Fanatic Member VBKNIGHT's Avatar
    Join Date
    Oct 2000
    Location
    Port25
    Posts
    619

    Resolved [RESOLVED] how to add handler to runtime created control

    Private Sub Form_Load()
    Dim ctrl1 as control
    Set ctrl1 = myfrm.Controls.Add("VB.CommandButton", "Team1", Me)
    ctrl1.Visible = True
    End Sub

    Sub TestClick()
    Msg "button clicked!"
    End Sub

    If a post has helped you then Please Rate it!

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: how to add handler to runtime created control

    There is WithEvents keyword that needs to be used if you want to event handler(s):
    Code:
    Option Explicit
    
    Dim WithEvents btn As CommandButton
    
    Private Sub Form_Load()
        Set btn = Me.Controls.Add("VB.CommandButton", "btnExit")
        btn.Move 1000, 1000
        btn.Caption = "Exit"
        btn.Visible = True
    End Sub
    
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
        Me.Controls.Remove "btnExit"
    End Sub
    
    Private Sub btn_Click()
        Unload Me
    End Sub

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: how to add handler to runtime created control

    Here are two options, there may be a couple more.

    1. Don't add VB controls that way, rather use a hidden dummy control, indexed at zero. Then dynamically load the controls using VB's Load method. You will have the built in handlers available to you.

    2. Declare the button's object in the declaration section. Edited: RhinoBull beat me to this one.
    Code:
    ' in declarations section
    Dim WithEvents myTestControl As VB.CommandButton
    
    Private Sub Form_Load()
    Set myTestControl = myfrm.Controls.Add("VB.CommandButton", "Team1", Me)
    myTestControl.Visible = True
    End Sub
    
    Private Sub myTestControl_Click()
        MsgBox "Got Click"
    End Sub
    This FAQ thread may be useful also

    There is yet another method using VB's VBControlExtender object, but that cannot be used with VB's intrinsic controls.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4

  5. #5

    Thread Starter
    Fanatic Member VBKNIGHT's Avatar
    Join Date
    Oct 2000
    Location
    Port25
    Posts
    619

    Re: how to add handler to runtime created control

    Thanks for all your answers this is very helpful. Godbless!

    If a post has helped you then Please Rate it!

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