Maintain TextBox selection despite window loosing focus?
-
Yep, i've done a fair amount of goggling with no success. Did anyone ever figure out a solution to this problem? The one solution that i tried was connecting to the textbox's lostfocus event, and then setting e.handled = true. Yep it didn't work xD
-
Yep, i've done a fair amount of goggling with no success. Did anyone ever figure out a solution to this problem? The one solution that i tried was connecting to the textbox's lostfocus event, and then setting e.handled = true. Yep it didn't work xD
You might want to try something like editing a text box's selected text style to include a border or a rectangle that displays on top of the selected text once some text is selected and focus is lost. Does this[^] custom control help by any chance?
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick Visit the Hindi forum here.
-
You might want to try something like editing a text box's selected text style to include a border or a rectangle that displays on top of the selected text once some text is selected and focus is lost. Does this[^] custom control help by any chance?
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick Visit the Hindi forum here.
I figured it out. Was pretty easy lol. Just added this to my main window to consume the WM_KILLFOCUS message, and by doing so i disabled the textbox from ever getting notified that focus for the app has been lost :D
const int WM_KILLFOCUS = 8; protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); HwndSource source = PresentationSource.FromVisual(this) as HwndSource; source.AddHook(WndProc); } IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (msg == WM_KILLFOCUS) handled = true; return IntPtr.Zero; }
Additionally, i also subscribed to the TextBox's LostFocus event in order to prevent the textbox from losing focus to another control, which would also result in the TextBox loosing the selection highlighting.private void textBoxReadingMaterial_LostFocus(object sender, RoutedEventArgs e) { e.Handled = true; //prevents selection from being lost }
modified on Sunday, October 24, 2010 1:54 AM
-
I figured it out. Was pretty easy lol. Just added this to my main window to consume the WM_KILLFOCUS message, and by doing so i disabled the textbox from ever getting notified that focus for the app has been lost :D
const int WM_KILLFOCUS = 8; protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); HwndSource source = PresentationSource.FromVisual(this) as HwndSource; source.AddHook(WndProc); } IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (msg == WM_KILLFOCUS) handled = true; return IntPtr.Zero; }
Additionally, i also subscribed to the TextBox's LostFocus event in order to prevent the textbox from losing focus to another control, which would also result in the TextBox loosing the selection highlighting.private void textBoxReadingMaterial_LostFocus(object sender, RoutedEventArgs e) { e.Handled = true; //prevents selection from being lost }
modified on Sunday, October 24, 2010 1:54 AM
-
I figured it out. Was pretty easy lol. Just added this to my main window to consume the WM_KILLFOCUS message, and by doing so i disabled the textbox from ever getting notified that focus for the app has been lost :D
const int WM_KILLFOCUS = 8; protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); HwndSource source = PresentationSource.FromVisual(this) as HwndSource; source.AddHook(WndProc); } IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (msg == WM_KILLFOCUS) handled = true; return IntPtr.Zero; }
Additionally, i also subscribed to the TextBox's LostFocus event in order to prevent the textbox from losing focus to another control, which would also result in the TextBox loosing the selection highlighting.private void textBoxReadingMaterial_LostFocus(object sender, RoutedEventArgs e) { e.Handled = true; //prevents selection from being lost }
modified on Sunday, October 24, 2010 1:54 AM
This is a pretty dangerous thing to do - focus management in WPF is nothing like focus control in Winforms because it has to manage focus in both the logical and visual tree.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
-
This is a pretty dangerous thing to do - focus management in WPF is nothing like focus control in Winforms because it has to manage focus in both the logical and visual tree.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
Ok, i was able to replace half of the solution with wpf-acceptable code, i.e. for a separate stackpanel, which contained controls which i didn't want to steal focus from my textbox, i simply set that stackpanel to have this property: FocusManager.IsFocusScope="true", and what i understand this did was create a separate focus-tree/scope/thing so those controls would not be able to affect my textboxes focus. (source: http://wpfhacks.blogspot.com/2009/06/correct-way-keep-selection-in-textbox.html) But so far i do not see an alternative to the following code, which prevents the textbox from being notified that the window lost focus:
#region WndProc in order to prevent the textbox from loosing focus when the window losses focus const int WM_KILLFOCUS = 8; protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); HwndSource source = PresentationSource.FromVisual(this) as HwndSource; source.AddHook(WndProc); } IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (msg == WM_KILLFOCUS) handled = true; return IntPtr.Zero; } #endregion
modified on Tuesday, October 26, 2010 5:38 PM