-3

Given the following event handler code:

private void image1_MouseDown(object sender, MouseButtonEventArgs e)
        {
            ///////////
        }

How can I call it from another method:

 private void timerTick(object sender, EventArgs e)
        {
            image1_MouseDown(object,  e); // error

        }
CoolBots
  • 4,770
  • 2
  • 16
  • 30

1 Answers1

0

Event handlers are regular methods like any others.

The reason you can't call it like you're trying to do, is because the MouseDown event takes a MouseButtonEventArgs instance, while the Tick event of timers take the base class EventArgs.

You'll need to create a new instance of MouseButtonEventArgs, but then you'll need to initialize it with a device, and other event data.

 

A better option would be to refactor the body of the MouseDown event handler to a method taking individual parameters, which you can then call without having to create a MouseButtonEventArgs instance.

private void image1_MouseDown(object sender, MouseButtonEventArgs e)
{
    this.ProcessImageMouseDown(e.ChangedButton, e.ButtonState, e.GetPosition(sender as FrameworkElement));
}

private void timerTick(object sender, EventArgs e)
{
    this.ProcessImageMouseDown(MouseButton.Left, MouseButtonState.Pressed, new Point(10d, 20d));
}

private void ProcessImageMouseDown(MouseButton button, MouseButtonState state, System.Windows.Point position)
{
    // Do actual processing here.
}
MulleDK19
  • 197
  • 1
  • 4