Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > Avalon

Problem with autoselecting text on getting focus

Closed Thread
 
Thread Tools Display Modes
Old 07-19-2007   #1 (permalink)
Sergey Aldoukhov
Guest


 

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?

Old 07-21-2007   #2 (permalink)
thelemmings@gmail.com
Guest


 

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

Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Text display problem HELP PLEASE Ray Vista General 10 07-25-2008 07:47 PM
WPF ListView focus problem. Igor .NET General 0 03-05-2008 07:01 AM
I have a problem with text-settings in Paint! indiced Vista music pictures video 6 01-08-2008 03:00 PM
keyboard problem: text goes everywhere AndrewV Vista hardware & devices 4 08-05-2007 11:16 AM
Text to Speech problem treesy Vista General 2 04-11-2007 04:42 AM








Vistax64.com 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 2005-2008

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 47 48 49 50