-1

A button's click event is fired when a user click this control. But this event can be fired by code , for example :

myButton_Click(myButton, EventArgs.Empty)

How can I distinguish these 2 cases ?

Thank you !

alex
  • 694
  • 3
  • 15
  • 35

4 Answers4

1

Create a new class that derives from RoutedEventArgs and pass that when you call the handler.

    public class MyExtendedRoutedEventArgs : RoutedEventArgs
{
   public bool ICalled {set;get;}
}

MyButton_Click(MyButton, new ExtendedRoutedEventArgs(){ICalled=true});

private void MyButton_Click(object sender, RoutedEventArgs e)
{
    if(e.getType() == typeof(MyExtendedRoutedEventArgs)
    {
        //you called it
    }
}
Vincent
  • 842
  • 7
  • 13
  • Thank you , I understand your code. But Is it possible not to use your class , but following idea : When I call the Button's click event from code , I use this instruction : myButton_Click(myButton, EventArgs.Empty) Now when this event is fired from a real user click , the EventsArg is empty or not ? Because if is not empty , I can just test inside the click event , if eventArgs is empty or not ? Can this idea work ? Thank you ! – alex Apr 06 '15 at 17:53
  • You could pass null as the second arg and check for null in the handler. if(e==null) but, the advantage to extending the class is that you could add any properties you want to that MyExtendedRoutedEventArgs and inspect those properties in the handler. – Vincent Apr 06 '15 at 18:04
  • I did now , because I was testing some things. But it seems that this is the only solution. – alex Apr 06 '15 at 19:32
0

As written, short of examining the stack trace via reflection you are probably out of luck.

Now you /could/ refactor it by creating a new sub that does the actual work with an extra parameter that will tell you how it was pressed. The event handler can call the new sub and pass a value that says it was fired via event, and the other "manual" call can pass a value that says it was called by your code. The new sub can then look at the value and take whatever action it needs to.

Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
  • Thank you. But the situation is a little complicated. I have another Event let's say Event2 that is fired when the button's click event is executed. But this event2 can be fired even without button's click event. And if this case is true , inside the event2 button's click event is called from code. So the vents are depending to each other , and I can't find other way but only to distinguish that the click's event is fired by user click or by code. – alex Apr 06 '15 at 16:56
  • If you are really desperate for a solution you can programatically check the stacktrace to find the calling frame (or step back multiple frame if you need to) to see if the call originated in your code or from inside the framework. "Real" button clicks should originate from inside the .NET framework. I really can't recommend doing this unless you have no other choice, It's a serious ugly hack. – Bradley Uffner Apr 06 '15 at 16:59
0

Pass something else as the sender or args when you call it programmatically.

enter code here
myButton_Click("I called it",null);

myButton_Click(object sender, RoutedEventArgs e)
{
   //Check the value of sender here
   if(sender.ToString() == "I called it" )
   {
       //You know you called it 
   }
}
Vincent
  • 842
  • 7
  • 13
  • ok , but I need the sender as object because I use some of its properties and also the .tag value. could you explain how can I do that without changing the sender , but with some args ? – alex Apr 06 '15 at 16:59
  • myButton.(property) unless you are using the same handler for multiple buttons. – Vincent Apr 06 '15 at 17:02
  • As long as you only use the `myButton_Click` method for one button only, you can directly access the control by it's name without the need of the `sender` variable. – Josh Part Apr 06 '15 at 17:02
  • I'm using a central click event for multiple buttons – alex Apr 06 '15 at 17:03
  • see new answer using args – Vincent Apr 06 '15 at 17:23
0

Would't it be easier to create a method that does the work, and call it from the Click event and/or from anywhere else in the code?

Private Sub DoTheThing(ByVal calledFromCode as boolean, ByVal sender as object)
    'Do the work here
End Sub

Private Sub myButton_Click(ByVal sender As Object, ByVal e As EventArgs)
    DoTheThing(false, sender)
End Sub

Private Sub SomeOtherMethod()
    DoTheThing(true, myButton)
End Sub
Josh Part
  • 2,154
  • 12
  • 15
  • Thank you , but as I have explained in an above comment the situation is a little complicated. I have another Event let's say Event2 that is fired when the button's click event is executed. But this event2 can be fired even without button's click event. And if this case is true , inside the event2 button's click event is called from code. So the events are depending to each other , and I can't find other way but only to distinguish that the click's event is fired by user click or by code. – alex Apr 06 '15 at 17:10
  • Sorry, I really don't follow. You have 2 events. If user clicks `myButton`, `Event1` is fired and this method calls `Event2` method. But if somewhere in your code you call `Event2`, this method has to call `Event1` - without calling again `Event2`? – Josh Part Apr 06 '15 at 17:18
  • I have only 2 events : Button's click event and Event1. Event1 is fired when the user click the button. In this case I need a way to not run some code inside the event1. Event1 can be fired from another way, not by user click on button. In this case at some point the button's click event is called by code. But if this is the case , inside button's click event I need a way to prevent some code from running. So as you can see all depend by the fact that the button's event is fired by code or by user . – alex Apr 06 '15 at 17:23
  • The confusing part is when you say you have `Button_Click` event and `Event1`, but `Event1` is **fired** when user clicks a button. Is `Event1` **really** tied to a `Button_Click` event (as `Private Sub Event1(ByVal sender As Object, ByVal e As EventArgs) Handles someButton.Click`)? Or you say it like that because you call the method `Event1` inside `myButton_Click`? Anyway, I think you should edit your question with the code from both events and show and example of how you want to call them. – Josh Part Apr 06 '15 at 17:48
  • no , Event1 is fired when the user click a button , because the code inside button's click event do something that cause event1 to be fired. – alex Apr 06 '15 at 17:51