-1

Please help.

I have a form and a class.

Form - frmTestTool

Class - MainClass

What I am trying to do is to print text everytime the mouse cursor is moved. So the scenario is, I have a software where I embedded the custom command. So I open the custom command and the form will pop up, I need to select somewhere in the software before clicking the "PlaceText" button in the form. After clicking the "PlaceText" button it will implement btnPlaceText_Click_1 but will no longer trigger "OnMouseMove".

Scenario 1(WORKING WELL steps)

  1. Select location in the software

  2. Open Custom Command

  3. Select Place Text

  4. Move MouseCursor (prints "Hello Word" every mouse move)

Scenario 2(NOT WORKING steps)

  1. Open Custom Command
  2. Select location in the software
  3. Select Place Text
  4. Move Mouse Cursor (this time, OnMouseMove does not triggered)

Here's the Code

 Partial Public Class frmTestTool
     Inherits Form
 
     Public Sub btnPlaceText_Click_1(sender As Object, e As EventArgs) Handles btnPlaceText.Click
       WriteMessage("Hello World")    
     End Sub 
 End Class


Public Class TextWizard
    Inherits BaseStepCommand

    Private Shared ofrmTestTool As frmTestTool = New frmTestTool()
    
    Public Overrides Sub OnSuspend()
        MyBase.OnSuspend()
    End Sub

    Public Overrides Sub OnResume()
        MyBase.OnResume()   
    End Sub 

    Public Overrides Sub OnStart(ByVal commandID As Integer, ByVal argument As Object)
        MyBase.OnStart(commandID, argument)
        Try
            m_running = True
            m_oTxnMgr = ClientServiceProvider.TransactionMgr
            ofrmTestTool = New frmTestTool()
            ofrmTestTool.Show()

        Catch commonException As CmnException
            ClientServiceProvider.ErrHandler.ReportError(ErrorHandler.ErrorLevel.Critical, MethodBase.GetCurrentMethod().Name, commonException, commandFailed)
        End Try
    End Sub
    
    Protected Overrides Sub OnMouseDown(ByVal view As GraphicView, ByVal e As GraphicViewManager.GraphicViewEventArgs, ByVal position As Position)
        MyBase.OnMouseDown(view, e, position)
        ofrmTestTool.btnPlaceText_Click_1(Nothing, Nothing)
    End Sub
End Class
Rjay
  • 3
  • 2
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Jul 01 '22 at 23:14

1 Answers1

0

I think you need to use RemoveHandler

RemoveHandler Me.MouseMove, AddressOf OnMouseMove

If you don't know the addresses of the handlers then you will need to use System.Reflection to find and remove them.

Sub RemoveEvents(Of T As Control)(Target As T, ByVal EventName As String)
    Dim oFieldInfo As FieldInfo = GetType(Control).GetField(EventName, BindingFlags.[Static] Or BindingFlags.NonPublic)
    Dim oEvent As Object = oFieldInfo.GetValue(Target)
    Dim oPropertyInfo As PropertyInfo = GetType(T).GetProperty("Events", BindingFlags.NonPublic Or BindingFlags.Instance)
    Dim oEvenHandlerList As EventHandlerList = CType(oPropertyInfo.GetValue(Target, Nothing), EventHandlerList)
    oEvenHandlerList.RemoveHandler(oEvent, oEvenHandlerList(oEvent))
End Sub

Call the Sub like this:

RemoveEvents(Me, "EventMouseMove")

' This can be used for event, just enter the string as "Event<EventName>" where the event name is the event you want to remove all handlers for on the target.

Fawlty
  • 451
  • 2
  • 9