Tuesday, February 26, 2008

Silverlight 2.0 Double Click Support

Ok, ok, I'm not giving anything away so don't get too excited. Check back at some later date. Who knows maybe MIX??? That is like next week.

Anyway, recently, I was told that you can't double-click in Silverlight...

So to me this is a bit hard to respond too. Not because Silverlight doesn't support it, but because if it supports single click or, 'OnMouseDown' -- 'OnMouseUp' or anything similar, then we are good. Basic computer science 101 stuff, right? All you need to do is something like this example.

In my code I create a counter for counting ticks. Say like this in c#:

public long LastTicks = 0;

Then in my 'MouseLeftButtonDown' event I have an 'if' statement that basically checks to see if the number of ticks between the last two mouse clicks is less then some defined number like this:


if ((DateTime.Now.Ticks - LastTicks) < 2310000)
{
// double click
here...
}

...and then at the end of the method, we have a line that sets the LastTicks value like this:

LastTicks = DateTime.Now.Ticks;

Nice and simple. This gives us a good solid double-click that we can do with whatever we need. Now in this sample we are using ticks but we could use any date-comparision thing and use seconds, for example, and we would just have to calculate the differential we want to use. For me it was 23.1 million ticks which seemed to work good on my box :)

Now don't go sending me a billion emails on why we don’t use double clicks in web apps. I'm just saying that it can be done, and a number of people asked. I know very well that double-click is not a good plan generally in a web sort of paradigm.

11 comments:

  1. Thank you very much for posting this hack. Double-click is highly important for the types of apps (web or not) that I develop.

    Joe

    ReplyDelete
  2. Simple, Clean and effective.

    Thank you very much.

    ... and it was my first link on the google!!! :-)

    ReplyDelete
  3. I needed that too! And thank you for keeping it simple.

    Note: I hate gays, and I hope you do too (as well as other people on this thread).

    ReplyDelete
  4. um... so simple is good but there was nothing in the post about 'gays'...? we should stict to the topic of the post.

    -David

    ReplyDelete
  5. It's a real shame that there are still people who are so irrational in this world. Please take your bigotry elsewhere.

    ReplyDelete
  6. this way you can't seperate single/double click.
    That's probably the reason you were told.

    ReplyDelete
  7. sure you can seperate the click and double click. its a function of how fast the clicks are. the code show clearly how you have two blocks that handle the two cases.

    ReplyDelete
  8. what if the control I want to double click doesn't fire the MouseLeftButtonDown event?
    another solution?

    ReplyDelete
  9. the same thing would apply to other events such as 'click' for a button etc.

    ReplyDelete
  10. This comment has been removed by a blog administrator.

    ReplyDelete
  11. Thanks for this tidbit, worked like a charm ;)

    ReplyDelete