Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - Problem with autoselecting text on getting focus

 
 
Old 07-19-2007   #1 (permalink)
Sergey Aldoukhov


 
 

Problem with autoselecting text on getting focus

For our application, the desired behavior is when a text box is
getting a focus, all text within that text box is automatically
selected, so user can just start typing, saving keystrokes. I couldn't
find a property that controls this behavior, so I just attached an
event to the text box:

edPrice.GotFocus += delegate(object sender, RoutedEventArgs e)
{
edPrice.SelectAll();
};
Unfortunately, this works only if the focus is received through
keyboard navigation, if a text box is selected with a mouse, the
selection disappears as soon as mouse button is released. Handling the
PreviewMouseLeftButtonUp (setting event argument's Handled to true)
helped, but completely broke mouse navigation. Re-selecting the text
in MouseLeftButtonUp also got me nowhere.

Any suggestions?


My System SpecsSystem Spec
Old 07-21-2007   #2 (permalink)
thelemmings@gmail.com


 
 

Re: Problem with autoselecting text on getting focus

Hi,

You could try something like the following, it's simple, but has
problem when losing the focus with the mouse. The next item will
require two mouse clicks instead of one to get the focus out of the
MyTextBox instance.

class MyTextBox: TextBox
{
bool isGettingFocus;

protected override void
OnPreviewMouseUp(System.Windows.Input.MouseButtonEventArgs e)
{
if (isGettingFocus)
{
isGettingFocus = false;
e.Handled = true;
}
else
base.OnPreviewMouseUp(e);
}

protected override void
OnGotKeyboardFocus(System.Windows.Input.KeyboardFocusChangedEventArgs
e)
{
base.OnGotKeyboardFocus(e);
this.SelectAll();
}

protected override void
OnPreviewGotKeyboardFocus(System.Windows.Input.KeyboardFocusChangedEventArgs
e)
{
base.OnPreviewGotKeyboardFocus(e);
isGettingFocus = true;
}
}

Luc

My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
wierd focus problem Microsoft Office
Text processing problem PowerShell
Text display problem HELP PLEASE Vista General
WPF ListView focus problem. .NET General
keyboard problem: text goes everywhere Vista hardware & devices


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46