none
2 MouseLeftButton Down events on same control?

    Question

  • I've been going through the quickstarts. I have an ellipse that I want to click once, stroke turns green, click it again, stroke goes back to gray. The only way I know how to get close to this is by changing the color with MouseLeftButtonDown, then changing it again with something different, like MouseEnter or something. How can I have two of the same event doing different things depending on what the color is?

    Tuesday, June 12, 2007 2:49 PM

Answers

  • As Swirlingmass pointed out above, this isn't going to work right for color values unfortunately, because you're not going to get "Gray" back as a stroke value... and that's my fault for leading you in that direction.

    For JS purposes, if this worked, you're saying "Does Current Color equal Gray? yes--set it to "Lime", no -- set it to "Gray"

    for the reasons Swirlingmass pointed out, I think I'd end up using a color variable to hold the color.

    Since you have no idea what browser you're on, you're going to end up manipulating numerical values... personally at that point, I'd end up going with a variable.

    Sorry for the confusion.

    -Dave

    Tuesday, June 12, 2007 6:02 PM

All replies

  • I've done this a couple ways...

    One, probably the best, would be to get the color from the object, and change it appropriately.

    The other way would be to keep a state variable of the current color.

    Keep asking questions if that doesn't help!

    -Dave

    Tuesday, June 12, 2007 5:29 PM
  • Thank you! Bear with me, I'm very new to programming in general. I figured I would have to do something like an if else statement depending on what the color is at that moment, but I'm new to javascript too and don't know the syntax to do any of this. I've fooled around trying to get something to work but nothing yet.

    By the way, I've been following your tutorials...great stuff!!!! keep 'em coming, they've helped me a great deal.

    Tuesday, June 12, 2007 5:33 PM
  • Thanks!

    You never really know how much to say, because a wide range of developers come here.

    I think I'd get the color into a variable something like

    var CurrentColor=sender.findName("ObjectName").Stroke;

    then I'd set it like this:

    sender.findName("ObjectName").Stroke=(CurrentColor="Red")?"Blue":"Red";  // if the current color is "Red", the stroke is set to "Blue" and vice-verse

    I think the syntax is right on that... if it's not, yell at me and I'll check it :)

    (I forgot the two colors you used ..)

    You could also get the ellipse as a variable, and avoid having to do sender.findName twice to get it done.

    I've learned more JS since doing Silverlight than the sum total of all I'd done before :)

    Keep asking questions!

    -Dave

    Tuesday, June 12, 2007 5:40 PM
  • A word of warning about getting colors:  you don't get a name, like "red" or "green", you get a number representing the color.  This number can be different in different browsers, most notably Firefox, which returns a negative number.  You need to add 16777216 to it to get the right number.

    Tuesday, June 12, 2007 5:43 PM
  • Good point... I'd forgotten about that Embarrassed

    -Dave

    Tuesday, June 12, 2007 5:48 PM
  • So I put it in like this:

    function power_on(sender, args) {

     

    var CurrentColor=sender.findName("powerButton").Stroke;

    sender.findName("powerButton").Stroke=(CurrentColor="Gray")?"Lime":"Gray"

    }

    It worked to set it to green when I clicked, but not back to gray or anything. I understand everything up to ?"Lime":"Gray"; what does that sytax mean? And I don't think it's working, unless I used it wrong.

    Tuesday, June 12, 2007 5:56 PM
  • As Swirlingmass pointed out above, this isn't going to work right for color values unfortunately, because you're not going to get "Gray" back as a stroke value... and that's my fault for leading you in that direction.

    For JS purposes, if this worked, you're saying "Does Current Color equal Gray? yes--set it to "Lime", no -- set it to "Gray"

    for the reasons Swirlingmass pointed out, I think I'd end up using a color variable to hold the color.

    Since you have no idea what browser you're on, you're going to end up manipulating numerical values... personally at that point, I'd end up going with a variable.

    Sorry for the confusion.

    -Dave

    Tuesday, June 12, 2007 6:02 PM