Knowing which object fired the message

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
swedishmichaell
Posts: 3
Joined: Sun Jun 15, 2014 11:33 am

Knowing which object fired the message

Post by swedishmichaell » Sun Jun 15, 2014 2:08 pm

Hi!
I have a somewhat very basic question, but I have not find the answer in my searches.

I have a main stack and a sub stack to that and on the sub stack I have two buttons.
In the main stack I have this simple code:

Code: Select all

on mouseUp
  answer "Button clicked"
end mouseUp
Of course the message appears if I click any of the two buttons, but how can I
separate them and know which one really was clicked?
Last edited by swedishmichaell on Sun Jun 15, 2014 2:36 pm, edited 1 time in total.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: Knowing which object fired the message

Post by bn » Sun Jun 15, 2014 2:17 pm

Hi swedishmichaell,

welcome to the forum.

look up "target" in the dictionary.

Code: Select all

on mouseUp
   answer the target
end mouseUp
Kind regards

Bernd

SparkOut
Posts: 2949
Joined: Sun Sep 23, 2007 4:58 pm

Re: Knowing which object fired the message

Post by SparkOut » Sun Jun 15, 2014 2:37 pm

and just for good measure, if you have not changed the name of the buttons so they are just called "Button" then

answer the [long] name of the target

and

answer the [long] id of the target

may also help.

swedishmichaell
Posts: 3
Joined: Sun Jun 15, 2014 11:33 am

Re: Knowing which object fired the message

Post by swedishmichaell » Sun Jun 15, 2014 4:15 pm

Thanks for your fast responds!

I came to this preliminary test code:

Code: Select all

on mouseUp
  if "Send" is in the target then
    answer "Send was clicked!"
  else if "Cancel" is in the target then
    answer "Cancel was clicked!"
  end if
end mouseUp
So now I can proceed with my tests. I have had LiveCode for "a couple of years" but never
had time to dive into it. It's about time now :D

Michael

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10356
Joined: Wed May 06, 2009 2:28 pm

Re: Knowing which object fired the message

Post by dunbarx » Sun Jun 15, 2014 4:29 pm

What everyone said.

My small contribution:

Do not, ever, use reserved words (or numbers) for the names of objects. "send", 3", "button", etc.

Note there is nothing wrong with using "button3" or "sendThis", that sort of thing.

Trust me.

Craig Newman

swedishmichaell
Posts: 3
Joined: Sun Jun 15, 2014 11:33 am

Re: Knowing which object fired the message

Post by swedishmichaell » Sun Jun 15, 2014 10:36 pm

Correct! Do not use reserved words. This became the final test code:

Code: Select all

on mouseUp
  if "btnSend" is in target then
    answer "Send was clicked!"
  else if "btnCancel" is in the target then
    answer "Cancel was clicked!"
  end if
end mouseUp

Post Reply