Results 1 to 9 of 9

Thread: Dynamic OCX Event does not fire

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    98

    Dynamic OCX Event does not fire

    I have a custom built ocx, when the ocx is dragged onto a form and I trigger the Event it works but when I recreate the control with either early or late binding I cannot triiger the Event.

    'Form version with ocx dragged onto form

    Private Sub Command1_Click()
    m_MyOcx.PerformOperation 9, 0
    'This is a dummy operation that causes the Event to be fired.
    End Sub

    Private Sub m_MyOcx_OperationCompleted(ByVal nOperationID As Long, ByVal nExitCode As Long, ByVal nUserID As Long)
    MsgBox "Fire!"
    End Sub


    The above works as intended.

    Below does not work


    Dim m_MyOcx As Object

    Private Sub Command1_Click()

    m_MyOcx.PerformOperation 9, 0

    End Sub

    Private Sub Form_Load()
    Set m_MyOcx = CreateObject("MyOcx.MyOcx.1")
    'Set m_MyOcx = New MyObjectName
    End Sub


    Private Sub m_MyOcx_OperationCompleted(ByVal nOperationID As Long, ByVal nExitCode As Long, ByVal nUserID As Long)
    MsgBox "Fire!"
    End Sub


    I've also used late and early binding with no positive results.
    Any help is appreciated.

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    VB Code:
    1. Dim WithEvents m_MyOcx As Object

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    98

    Tried that too! No go..

    I forgot to mention I used that in my early binding attempt and it didn't work. I might add that the other properties and methods function as expected.

  4. #4
    Fanatic Member
    Join Date
    Feb 2003
    Location
    Los Angeles, CA
    Posts
    681
    the 2nd sample that you have is late binding, and (i may be wrong here) i dont think you can use events with late binding. if you want to use early binding, the only way to go is
    VB Code:
    1. Dim WithEvents myOCXInstance As myOCX.myClassInThere
    2.  
    3. Set myOCXInstance = New myOCX.myClassInThere
    note the WithEvents keyword above. this *doesnt* work:
    VB Code:
    1. Dim WithEvents myOCXInstance As New myOCX.myClassInThere
    i.e. WithEvents cannot be used in a Dim ... As New.

    you may wanna try this late binding as well:
    VB Code:
    1. Dim WithEvents myOCXInstance As Object
    2. Set myOCXInstance = CreateObject("MyOcx.MyOcx.1")
    but then again, i am not at all sure that it's gonna work. if it doesnt, your only way out are callback functions. (yes, they do exist in visual basic.)
    there are 2 reasons why i leave my work unfinished:
    (1) i'm getting old.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    When you Dim something as Object.... an Object doesn't have events.... it only is... so I don't think a Dim WithEvents As Object will allow any events to fire off.
    The _only_ way I know to get events is Dim WithEvents something As MyObject and then somewhere Set something = New MyObject.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    This is from MSDN and maybe it will get you started. I believe you will have to declare the object as vbControlExtender and the check the return from the ObjectEvent parameter.
    Add Method (Controls Collection)


    Adds a control to the Controls collection and returns a reference to the control.

    Syntax

    object.Add (ProgID, name, container)

    The Add method syntax has these parts:

    Part Description
    object Required. Anobject expression that evaluates to an object in the Applies To list.
    ProgID Required. A string that identifies the control. The ProgID of most controls can be determined by viewing the Object Browser. The ProgID is composed of the Library and Class of the control. For example, the CommandButton control's ProgID is VB.CommandButton. In cases where the ProgID differs from that shown in the Object Browser, Visual Basic displays an error message that contains the correct ProgId.
    name Required. A string that identifies the member of the collection.
    container Optional. An object reference that specifies a container of the control. If not specified or NULL, defaults to the container to which the Controls collection belongs. You can put a control in any existing container control (such as the Frame control) by specifying this argument. A user control or an ActiveX document can also be a container.


    Remarks

    Note The Controls collection is a late-bound collection. This means the compiler cannot determine in advance which controls are contained by the collection, their objects or their interfaces. Without this information, the Auto Statement Builder will not function.

    This method allows you to add controls to an application at run time. Dynamic control addition can be used to add the functionality of a control to an application, even after the application has been compiled and deployed. For example, you may have several complex user controls, each suited to a different task. Depending on an external factor, such as time or date or user input, a different user control could be added to an existing form in an application. You can also use the container argument of the method to specify a container control (such as the Frame control) to position the control. Or you can design an application that automatically reads a file, database, or registry entry for new controls to load. In this way, you can modify an application without having to redeploy it.

    Important When you add an unreferenced control that requires a license to an existing (deployed) application, you must also add the license key for the control before using the Add method. For information on when and how to add licenses, see "Licenses Collection" in the See Also list.

    Adding Unreferenced Controls at Run Time
    You can also use the Add method to dynamically add a control that is not referenced in the project. (An "unreferenced" control is a control that is not present in the Toolbox.) To do so, you must also add the control's License key to the Licenses collection as well. The example below adds a control's license key before adding the control itself:

    Option Explicit
    Private WithEvents extCtl As VBControlExtender

    Private Sub Form_Load()
    Licenses.Add "prjWeeks.WeeksCtl", "xydsfasfjewfe"
    Set extCtl = Form1.Controls.Add("prjWeeks.WeeksCtl", "ctl1")
    extCtl.Visible = True ' The control is invisible by default.
    End Sub

    Note See Add Method (Licenses Collection) in the See Also list for more information about retrieving a control's license key.

    In order to program the events of such an unreferenced control, however, you must declare an object variable using the WithEvents keyword as a VBControlExtender object (shown above), and set the object variable to the reference returned by the Add method. Then use the VBControlExtender object's ObjectEvent event to program the control's events. An abbreviated example is shown below.

    Option Explicit
    Dim WithEvents objExt As VBControlExtender ' Declare VBControlExtender variable

    Private Sub LoadControl()
    Licenses.Add "Project1.Control1", "xydsfasfjewfe"
    Set objExt = Controls.Add("Project1.Control1", "myCtl")
    objExt.Visible = True
    End Sub

    Private Sub extObj_ObjectEvent(Info As EventInfo)
    ' Program the events of the control using Select Case.
    Select Case Info.Name
    Case "Click"
    ' Handle Click event here.
    ' Other cases now shown
    Case Else ' Unknown Event
    ' Handle unknown events here.
    End Select
    End Sub

    Note You can't assign an intrinsic control to the VBControlExtender variable; any attempt will result in a type mismatch error.

    You can also program the events of a dynamically added control by declaring an object variable using the WithEvents keyword, and setting the reference returned by the method to the variable, as shown below:

    Option Explicit
    ' Declare object variable as CommandButton.
    Private WithEvents cmdObject As CommandButton

    Private Sub Form_Load()
    Set cmdObject = Form1.Controls.Add("VB.CommandButton", "cmdOne")
    cmdObject.Visible = True
    cmdObject.Caption = "Dynamic CommandButton"
    End Sub

    Private Sub cmdObject_Click()
    Print "This is a dynamically added control"
    End Sub

    If you intend to add a user control or any ActiveX control to your form, you must either add the control to the Toolbox, or add its License key to the Licenses collection. See the Add Method (Licenses Collection) for more information.

    Note If you add an ActiveX or user control to your project but don't use it on a form, you must also uncheck the Remove Information About Unused ActiveX Controls option on the Make tab of the Project Properties dialog box. If your application attempts to add the control, the Add method will fail because the necessary information has been discarded.

    Removing Controls
    To remove any controls added dynamically, use the Remove method. It should be noted that you can only remove controls added using the Add method (in contrast to controls added using the Load statement). The example below removes a dynamically added control:

    Form1.Controls.Remove "ctl1" ' The control's name is ctl1.

  7. #7
    Fanatic Member
    Join Date
    Feb 2003
    Location
    Los Angeles, CA
    Posts
    681
    Originally posted by MarkT
    This is from MSDN and maybe it will get you started. I believe you will have to declare the object as vbControlExtender and the check the return from the ObjectEvent parameter.
    excellent point!
    there are 2 reasons why i leave my work unfinished:
    (1) i'm getting old.

  8. #8
    New Member
    Join Date
    Aug 2006
    Posts
    1

    Re: Dynamic OCX Event does not fire

    Ok, I'm in a similar situation except that I'm using a VBControlExtender and it still doesn't work. Here's what the code looks like:

    Project1.UserControl1:
    VB Code:
    1. Private WithEvents m_ctlDynamicDevice   As VBControlExtender
    2.  
    3. Public Sub SomeSub()
    4.  
    5.     Set m_ctlDynamicDevice =     UserControl.Controls.Add("Project2.UserControl2", "DynDev")
    6.  
    7. End Sub
    8.  
    9. Private Sub m_ctlDynamicDevice_ObjectEvent(info As EventInfo)
    10.     If info.Name = "OnSomeEvent" then
    11.         'handle the event
    12.     End If
    13. End Sub
    Project2.UserControl2:
    VB Code:
    1. Event OnSomeEvent
    2.  
    3. Public Sub TriggerEvent
    4.     RaiseEvent OnSomeEvent
    5. End Sub

    I can step into the TriggerEvent routine, but when the event is raised, I can't step into the ObjectEvent routine. Also, UserControl.Controls.Add returns a valid object and that object does not change between the time its added and the time the event handler should be called.

  9. #9
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Dynamic OCX Event does not fire

    See if the attached sample helps you.
    Attached Files Attached Files

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