7 Replies Latest reply on Mar 23, 2016 3:09 PM by Patrick.Cashman

    How can I attach events in Microsoft Visual Basic when Scripting.Attachevents is not recognized?

    mgeisler

      I am using Microsoft Visual Basic to write my Expedition automation utility and I cannot attach events using the Scripting.Attachevents method. Is there another way that I can attach events in Visual Basic?

        • 1. Re: How can I attach events in Microsoft Visual Basic when Scripting.Attachevents is not recognized?
          Patrick.Cashman

          Let's say you have a class that has eventhandlers in it.  Mousehook is a good example.  Such as is described here.

           

          Somewhere else, say in a form's class, you will declare an instance of the mousehook with a statement like this:

           

          Private WithEvents mHook As New MouseHook

           

          Then, to actually invoke it, do this:

           

          mHook.HookMouse()

           

          Then you will have several subroutines in your form class to tell what to do in that form when the event fires:

           

          Private Sub mHook_Mouse_Wheel(ByVal Direction As MouseHook.Wheel_Direction) Handles mHook.Mouse_Wheel

                  If in_box = True Then

                      If Direction.ToString = "WheelUp" Then zoom = zoom - zoom_increment

                      If Direction.ToString = "WheelDown" Then zoom = zoom + zoom_increment

                      glControl1.Invalidate()

                  End If

              End Sub

           

          The handles statement at the end of the Subroutine declaration ties the event handler in HookMouse to the action captured in the form, so that the Subroutine is executed when the event fires. The name of the subroutine is not important, but the Handles part has to refer to the Event in HookMouse with that name:

           

          Public Event Mouse_Wheel(ByVal Direction As Wheel_Direction)

           

          So this is a basic overview, with what I hope is a valid example of how to do it.  There are other pertinent details that have to be included, such as all the appropriate imports statements and dll calls that make something like this work; in this case User32.dll which is what Windows uses to interact with the mouse. In your case you can define your event handler any way you want, using whatever resources you need to.

           

          Hope this helps.

          • 2. Re: How can I attach events in Microsoft Visual Basic when Scripting.Attachevents is not recognized?
            mgeisler

            I cannot correlate your example to what I should do in MS Visual Basic with the code below.

            It typically would be done like this in vbs or in the IDE script form editor...

             

             

            ' Get the application object
            Dim pcbApp: Set pcbApp = GetObject(,"MGCPCB.ExpeditionPCBApplication")

            ' Attach events from the application object
            Call Scripting.AttachEvents(pcbApp, "appEvents")

            Somehow, I need to bind the application object (pcbApp) events to an event handler like OnOpenDocument or Quit.

            What would be the syntax for attaching pcbApp to appEvents in MS VB?

             

            • 3. Re: How can I attach events in Microsoft Visual Basic when Scripting.Attachevents is not recognized?
              Patrick.Cashman

              I don't think you need a statement like that if you are accessing the automation COM.

               

              Once you have created the pcbapp object, all of the events are immediately available.  Such as:

               

              pcbapp.OnOpenDocument

               

              Now, I see that you are using MS Visual Basic.  I assume this means VB6.  If not, please clarify.  In the case of VB6, it has been many years since I used it and don't remember exactly if all the methods, events, and properties of an object pop up as you are typing like they do with .net.  If they don't then you are going to have a very hard time working because many of them are not documented anywhere and you will spend most of your time chasing down syntax issues and trying to find the thing you need. In .net everything appears as you type, making it much easier to see what is available and what is not.

               

              I recommend that you upgrade to vb.net.  The vast majority of what is available in terms of help and examples will pertain to .net and not to vb6.  And it's much more powerful in many ways.

               

              Sorry if I'm on the wrong track here.

              • 4. Re: How can I attach events in Microsoft Visual Basic when Scripting.Attachevents is not recognized?
                matthias.cosaert

                Here is some code I used on a VB.net project that uses events:

                 

                 

                Dim WithEvents appPCB As MGCPCB.Application
                
                Private Sub form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                     appPCB = Nothing
                     On Error Resume Next
                     appPCB = GetObject(,"MGCPCB.Application")
                     On Error GoTo 0
                     If appPCB Is Nothing Then
                          Me.Close()
                 
                     End If
                End Sub
                 
                Private Sub Application_OnOpenDocument(ByVal Flags As MGCPCB.EPcbOnOpenDocumentFlags) Handles appPCB.OnOpenDocument
                      MsgBox("open") 
                 End Sub
                

                • 5. Re: How can I attach events in Microsoft Visual Basic when Scripting.Attachevents is not recognized?
                  mgeisler

                  I am using MS Visual Basic Express 2010 and I cannot get your example to work.

                  • 7. Re: How can I attach events in Microsoft Visual Basic when Scripting.Attachevents is not recognized?
                    Patrick.Cashman

                    In my previous post, I misunderstood the issue.  Here's another example that works:

                     

                    Public WithEvents app as ViewDraw.Application

                     

                    Public Sub mousemoved(ByVal Flags As Long, ByVal X As Long, ByVal Y As Long) Handles app.MouseMoved

                         x1 = CDbl(X / 25400)

                         y1 = CDbl(Y / 25400)

                    End Sub