Results 1 to 3 of 3

Thread: Runtime created controls

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2000
    Posts
    8

    Question

    I am dynamically creating checkboxes during runtime. When the user selects the checkbox or double clicks the checkbox, I would like to call a predefined subroutine.

    How do I access a controls events when the control is created during run time?
    Vin Lis

  2. #2
    Guest
    You can achieve this by SubClassing.

    Code for a Module
    Code:
    '*****************************************************************************
    '
    '                             DYNAMIC EVENTS
    '
    '                 Written by Randy Jheeta, April 2000
    '   -=Please do not modify/reproduce this code without contacting me first=-
    '
    '                       E-mail: [email protected]
    '
    '*****************************************************************************
    
    Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Public Const GWL_WNDPROC = (-4)
    Public Const WM_COMMAND = &H111
    Public Const WM_CLOSE = &H10
    Public tmpParam As Long
    Public g_OldProc As Long
    
    Public Function WindowProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        
        Select Case Msg&
        
            Case WM_COMMAND:
                tmpParam = LoWord(lParam)
                
                Select Case tmpParam
                    Case frmMain.Check1.hwnd
                        'Check1 was clicked
                        Call MsgBox("You clicked Check1")
                    Case frmMain.Check2.hwnd
                        'Check2 was clicked
                        Call MsgBox("You clicked Check2")
                End Select
            
            Case WM_CLOSE:
                Call SetWindowLong(hwnd&, GWL_WNDPROC, gOldProc&)
        
        End Select
        
        WindowProc = CallWindowProc(g_OldProc&, hwnd&, Msg&, wParam&, lParam&)
    
    End Function
    
    Public Function HiWord(wParam As Long) As Integer
        If wParam And &H80000000 Then
            HiWord = (wParam \ 65535) - 1
        Else
            HiWord = wParam \ 65535
        End If
    End Function
    
    Public Function LoWord(wParam As Long) As Integer
        If wParam And &H8000& Then
            LoWord = &H8000 Or (wParam And &H7FFF&)
        Else
            LoWord = wParam And &HFFFF&
        End If
    End Function
    Code for Form
    Code:
    Private Sub Form_Load()
    
        'Create Check1
        Controls.Add "VB.CheckBox", "Check1"
        Me!Check1.Move 0, 0
        Me!Check1.Caption = "Check1"
        Me!Check1.Visible = True
    
        'Create Check2
        Controls.Add "VB.CheckBox", "Check2"
        Me!Check2.Move 480, 480
        Me!Check2.Caption = "Check2"
        Me!Check2.Visible = True
    
       g_OldProc& = GetWindowLong(Me.hwnd, GWL_WNDPROC)
       Call SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf WindowProc)
       
    End Sub

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    You can also achieve this by simply Declaring an object with the WithEvents keyword like this:
    Code:
    Private WithEvents chkBox As CheckBox
    
    Private Sub Form_Load()
        Set chkBox = Me.Controls.Add("VB.Checkbox", "MyNewCheckBox")
    End Sub
    
    Private Sub chkBox_Click()
        'call your sub from here
    End Sub
    Good luck!

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