Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Maintain TextBox selection despite window loosing focus?

Maintain TextBox selection despite window loosing focus?

Scheduled Pinned Locked Moved WPF
helpquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    FocusedWolf
    wrote on last edited by
    #1

    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

    A 1 Reply Last reply
    0
    • F FocusedWolf

      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

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      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.

      F 1 Reply Last reply
      0
      • A Abhinav S

        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.

        F Offline
        F Offline
        FocusedWolf
        wrote on last edited by
        #3

        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

        A P 2 Replies Last reply
        0
        • F FocusedWolf

          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

          A Offline
          A Offline
          Abhinav S
          wrote on last edited by
          #4

          Thanks for posting your solution here.

          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.

          1 Reply Last reply
          0
          • F FocusedWolf

            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

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            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

            My blog | My articles | MoXAML PowerToys | Onyx

            F 1 Reply Last reply
            0
            • P Pete OHanlon

              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

              My blog | My articles | MoXAML PowerToys | Onyx

              F Offline
              F Offline
              FocusedWolf
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups