Matthias Shapiro

I need fewer hobbies

Getting a MouseLeftButtonDown or MouseLeftButtonUp Event From Your TextBox

One of the nifty little tricks I got working in my attempt to update my Silverlight Color Picker was that I wanted to have the entire content of a TextBox to select when I clicked on it. Imagine my surprise when clicking on the TextBox didn’t fire either a MouseLeftButtonUp or a MouseLeftButtonDown event!

This stunned me a little bit. Turns out there is a big discussion about this over at the Silverlight Forum. Long story short, I needed to add this code to my project (you’ll see it in my Color Picker project if you download it).

Add this code to your MyPage.xaml.cs file:

public class TextBoxClickable : TextBox
{
     protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
     {
           base.OnMouseLeftButtonDown(e);
           e.Handled = false;
     }

     protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
     {
           base.OnMouseLeftButtonUp(e);
           e.Handled = false;
     }
}

Your XAML should define your namespace:

<UserControl x:Class=”MyProjectName.MyPage
      xmlns:clickable=”clr-namespace:MyPage“>

And you can use your new clickable TextBox like this:

<clickable:TextBoxClickable MouseLeftButtonDown=”TextBox_SelectAll/>

If you’re like me and want to select everything in the TextBox with a single click, your method will look like this:

private void TextBox_SelectAll(object sender, MouseButtonEventArgs e)
{
     TextBox selectionBox = (TextBox)sender;
     selectionBox.SelectAll();
}

That’s it. I’ll post my updated color pickers in a couple hours.

This entry was posted in colorpicker, Silverlight, Tutorial, XAML and tagged , , , . Bookmark the permalink.

0 Responses to Getting a MouseLeftButtonDown or MouseLeftButtonUp Event From Your TextBox

  1. Mounir

    It does not work if you use under ScrollViewer. You can try it !

    For resolve this bug, you should modify e.Handled = false;
    by e.Handled = !base.Focus();

  2. san san

    nice, thank you…..

  3. cal

    I have tried this approach and have had one set back. In my case when the user clicks a textbox it’s going to see if that text is a valid uri, if so it’s going to open that uri. However, I would still like the textbox to receive focus and allow the user to edit the contents. Using the above approach is not giving the textbox focus?

  4. Chris, this is true, but I was writing this is regard to a Silverlight application, which doesn’t have the Preview set of events (thus requiring the extra work).

  5. Another way to do this is to use the PreviewMouseLeftButtonDown and Up within the TextBox element. The reason that it won’t fire the regular MouseButtonDown and Up is that they are being handled by the system already to allow you to click to focus and then enter text.

  6. Josiah

    Seems like a rather unnecessarily complicated procedure just to get the mouseDown and mouseUp events to work properly. Is there a good reason for Microsoft to give us this headache?

  7. Pingback: Designer WPF » Blog Archive » Updated Silverlight Color Picker Gadget