Jens
Jens

Reputation: 6375

Change order in which a mouse event is handled

I hope I can make it clear what my problem is. I have made my own Chart control

Public Class MyChart
  Inherits Chart
  [...]
End Class

In this class I handle the MouseDown event of the chart and do stuff like zooming, dragging markers and so on.

In the project I am using the chart I also handle the MouseDown event to do some more specific stuff. Now there is this problem:

In the MouseDown event handler in the MyChart class I use the middle mouse button to drag the chart.

However in the project MouseDown event handler I check if the user hit a specific object and want to let him drag this object also with the middle mouse button. The problem is, that the handler in MyChart is executed first so I can't check if the user hit the object (and therefore initiate the dragging of this object).

What I need is that the event handler in the project is executed first and after this the one in MyChart.

To hopefully make it more clear:

(Dummy Code)
Public Class SomeProject
  WithEvents Chart as MyChart
  Public Sub Chart_MouseDown(sender as object, e as MouseEventArgs) Handles Chart.MouseDown
    'This is executed second, but should be executed first.
  End Sub
End Class

Public Class MyChart
  Inherits Chart
  Public Sub MyChart_MouseDown(sender as object, e as MouseEventArgs) Handles Me.MouseDown
    'This is executed first, but should be executed second.
  End Sub
End Class

Is there any way I can do this?

Upvotes: 3

Views: 585

Answers (1)

LarsTech
LarsTech

Reputation: 81610

Try creating your own event:

Public Class MyChart
  Inherits Chart

  Public Event ChartMouseDown(sender As Object, e As MouseEventArgs)

  Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
    RaiseEvent ChartMouseDown(Me, e)
    MyBase.OnMouseDown(e)
    // your second code here...
  End Sub  
End Class

Then your project code would look like this:

Public Class SomeProject
  WithEvents Chart as MyChart
  Public Sub Chart_ChartMouseDown(sender as object, e as MouseEventArgs) _
                                  Handles Chart.ChartMouseDown
    // your first code here...
  End Sub
End Class

Upvotes: 2

Related Questions