Results 1 to 4 of 4

Thread: How to differentiate between event triggered by user interaction and code ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2020
    Posts
    70

    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
    Last edited by IT_Researcher; Jul 7th, 2024 at 11:12 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    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.
    My usual boring signature: Nothing

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,262

    Re: How to differentiate between event triggered but user interaction and code ?

    Quote Originally Posted by Shaggy Hiker View Post
    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?
    Last edited by Zvoni; Jul 8th, 2024 at 02:17 AM.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width