Select all text in a textbox when the user clicks to focus

A tiny patch of code can sometimes save your users a lot of time. In a project I was working on, I used a lot of small textboxes to have the user input small numeric values. (Actually the textbox was a part of a kind of self-constructed NumericUpDown, but that’s another story.) Watching a tester work with the program, I saw him often clicking in the box and then starting typing his numbers, becoming increasingly annoyed that the numbers would be inserted at the position he clicked instead of overwriting the number that was already there.

Selecting all text in a box is nice behavior when you Tab into it, but in general I don’t like it for clicking in the box, because I expect the caret to be where I put it. There are exceptions to that: the browser address bar is one of them and that makes sense, because you more often than not want to overwrite the entire address instead of inserting or modifying parts. And, come to think about it, because these were just small numbers (up to 4 positions), I had to agree with the tester that it if you want to change a number, it would be a lot easier to just type over it, than it would be to start inserting or modifying digits.

So I wrote on override for the OnEnter event and executed a select all there. Done! That was easy. Or not… It didn’t work.

I tried attaching a handler to OnClick. Well, that didn’t work as expected either.

The fast-gun alert in his question exactly matched my two failed attempts at a quick fix, so that looked promising. 🙂 With the help of the responders he eventually nailed it by hooking up just three small event handlers to the MouseUp, GotFocus and Leave events. I just added SelectAllOnFocus, a boolean property to be able to turn this behavior on and off.

Works great! In The Netherlands, there was a snail mail company that had a slogan about sending someone birthday cards: “kleine moeite, groot plezier” (Small effort, lots of fun). I hope that by adding this small piece of code I made a lot of my users happy.

Advertenties

Share this:

Vind ik leuk:

Gerelateerd

Over ShiftkeyI am Maarten, owner of and chief software developer for Shiftkey software development from The Netherlands. I will be writing mainly about things I run into when programming in C# or Delphi.