Page 1 of 1

Knowing which object fired the message

Posted: Sun Jun 15, 2014 2:08 pm
by swedishmichaell
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?

Re: Knowing which object fired the message

Posted: Sun Jun 15, 2014 2:17 pm
by bn
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

Re: Knowing which object fired the message

Posted: Sun Jun 15, 2014 2:37 pm
by SparkOut
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.

Re: Knowing which object fired the message

Posted: Sun Jun 15, 2014 4:15 pm
by swedishmichaell
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

Re: Knowing which object fired the message

Posted: Sun Jun 15, 2014 4:29 pm
by dunbarx
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

Re: Knowing which object fired the message

Posted: Sun Jun 15, 2014 10:36 pm
by swedishmichaell
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