How to differentiate between event triggered by user interaction and code ?
I am looking for a way to determine if an event was triggered by user interaction or through code. I have seen suggestions in other forums to use a flag and set it accordingly when triggering the event programmatically. However, I am curious if there are any built-in methods or alternative approaches to achieve this.
Referred links:
https://stackoverflow.com/questions/...103966#3103966
https://www.vbforums.com/showthread....event-is-fired
Re: How to differentiate between event triggered but user interaction and code ?
What event? If you're talking about user interaction then you're generally talking about controls. It would depend on the event but, generally speaking, no. For instance, the TextChanged event simply indicates that the Text property value has changed. How that happens is irrelevant to the event. The event would be raised in the setter of the Text property and there's no way for the code to know how it got there. Also, if there was a way to distinguish how the event was raised, that information would be in the 'e' parameter of the event handler, so any event handler for which the data type of that parameter is EventArgs obviously has no means to provide that information.
Re: How to differentiate between event triggered but user interaction and code ?
If the code that triggers the event is code that you wrote, then a Boolean would certainly work. The code could set the Boolean, but a user would not, so if the Boolean is set, then your code did it. If the Boolean is not set, then the user did it.
Re: How to differentiate between event triggered but user interaction and code ?
Quote:
Originally Posted by
Shaggy Hiker
If the code that triggers the event is code that you wrote, then a Boolean would certainly work. The code could set the Boolean, but a user would not, so if the Boolean is set, then your code did it. If the Boolean is not set, then the user did it.
Which would require such a Variable to have at least module-scope, since you can only set such a variable outside the Event itself.
If there are multiple events involved OP wants to control if User-Interaction or from Code, it would require one such variable per Event (or an array, Bitfield, whatever)
What about a custom Event-Handler?