Detect which control triggered an event

hope you can give me a hint on the following …

Two buttons on a form. When pressed, they “do” different things but both also call the same method.
The method has an “if” construct to execute either and INSERT or an UPDATE sql statement.

The method should detect which button pressed event did call the method.
I guess I could add a parameter to the method and pass an argument to the method call to identify the button, but I just wonder if once can detect the control that triggered an event.

Thanks in advance!

Yes, this is possible. Let me check if I can find the code.

Edit: you make an exception and parse the information from the stack:

#Pragma BreakOnExceptions False
#if DebugBuild then
    dim f as FolderItem
  catch err as NilObjectException
    dim theCleaner as new ErrorExceptionCleaner
    dim ExceptionStack as String = theCleaner.CleanStack(err)
    Globals.theErrorLog.LogItem("Stack for prepare quit: " + EndOfLine.CR + ExceptionStack + EndOfLine.CR)
  end try
#Pragma BreakOnExceptions True

This makes some entries in the log:

2024-04-06 07:58:16 Stack for prepare quit:
Sub MaxModel.Notify(string)
Sub NotificationCenter.Post(string, string)
Sub NotificationManager.Post(string)
Sub ErrorException.Constructor(RuntimeException, string)
Function MainWindow.MainWindow._FileQuit_Action(MainWindow.MainWindow) as boolean
Sub Application._CallFunctionWithExceptionHandling()

I would say it’s easier with a parameter.

1 Like

Thanks for your fast answer first of all.
I am not so keen on xojo yet so I don’t understand your code yet but I will with the help of the documentation.
Meantime I will probably go on with a parameter until I am able to understand and implement your code :slight_smile:
Thanks again!

You could create a Control set for these buttons, so they are an Array with an index then.

Or create your own array of controls and use Addcontrol and Addhandler, like

Window1.AddControl mybutton(mybutton.LastIndex)
AddHandler mybutton(mybutton.LastIndex).Pressed, AddressOf myhandler

1 Like

i would pass CurrentMethodName as Argument to your method
or the Me Object and read the Tag Propertie.

“if” construct to execute either and INSERT or an UPDATE sql statement

i use a id of 0 for a new record (insert) and pass the id back to the input form.
id > 0 is always an update.

and firebird database have a smart “update or insert into” with a single query.

1 Like

In any case you should pass parameters. Coupling or decoupling the sender.

Decoupling you can use a command pattern, coupled you extend the event passing the sender.



Coupled to the sender (identifying the source):


Thanks a lot to all of your for your answers!
The support from this community is really fantastic!