Unselect text in textbox on textbox lostfocus event.
-
I am working in WPF. Normally on lost focus of textbox , text are unselected. But if I set the value e.handled = true on lostfocus event handler, unselection doesn't happen. I need to set e.handled = true to prevent the event from bubbling up. Note: I am not stopping from lostfocus being triggered. LostFocus takes place, its just that, automatic unselection doesn't happen and I want the unselection to take place. Private Sub MyTextBox_LostFocus(ByVal sender As Object, ByVal e As RoutedEventArgs) e.Handled = True End Sub
-
I am working in WPF. Normally on lost focus of textbox , text are unselected. But if I set the value e.handled = true on lostfocus event handler, unselection doesn't happen. I need to set e.handled = true to prevent the event from bubbling up. Note: I am not stopping from lostfocus being triggered. LostFocus takes place, its just that, automatic unselection doesn't happen and I want the unselection to take place. Private Sub MyTextBox_LostFocus(ByVal sender As Object, ByVal e As RoutedEventArgs) e.Handled = True End Sub
Try setting the TextBox.SelectionLength property to 0 in your LostFocus handler method.
Blog: http://windowsclientdevelopment.spaces.live.com FAQs: http://windowspresentationfoundation.wikispaces.com http://windowsmobile.wikispaces.com http://vsto.wikispaces.com
-
Try setting the TextBox.SelectionLength property to 0 in your LostFocus handler method.
Blog: http://windowsclientdevelopment.spaces.live.com FAQs: http://windowspresentationfoundation.wikispaces.com http://windowsmobile.wikispaces.com http://vsto.wikispaces.com
Well setting that only leaves the textedit cursor blinking in the text box even after lost focus. I have already tried via default selection property settings e.g selectionlength or selectionstart, all results in setting the cursor blinking in the text box even if the next control get focus. Just tab through the text box and see the behviour of the cursor on txtFirst and txtSecond < textbox x:name="txtTest" lostfocus="txtFirst_LostFocus" xmlns:x="#unknown" / > < textbox x:name="txtFirst" lostfocus="txtFirst_LostFocus" xmlns:x="#unknown" / > < textbox x:name="txtSecond" lostfocus="txtSecond_LostFocus" xmlns:x="#unknown" / > < textbox x:name="txtFourth" xmlns:x="#unknown" / > Private Sub txtFirst_LostFocus(ByVal sender As Object, ByVal e As RoutedEventArgs) e.Handled = True End Sub Private Sub txtSecond_LostFocus(ByVal sender As Object, ByVal e As RoutedEventArgs) e.Handled = True End Sub
-
Well setting that only leaves the textedit cursor blinking in the text box even after lost focus. I have already tried via default selection property settings e.g selectionlength or selectionstart, all results in setting the cursor blinking in the text box even if the next control get focus. Just tab through the text box and see the behviour of the cursor on txtFirst and txtSecond < textbox x:name="txtTest" lostfocus="txtFirst_LostFocus" xmlns:x="#unknown" / > < textbox x:name="txtFirst" lostfocus="txtFirst_LostFocus" xmlns:x="#unknown" / > < textbox x:name="txtSecond" lostfocus="txtSecond_LostFocus" xmlns:x="#unknown" / > < textbox x:name="txtFourth" xmlns:x="#unknown" / > Private Sub txtFirst_LostFocus(ByVal sender As Object, ByVal e As RoutedEventArgs) e.Handled = True End Sub Private Sub txtSecond_LostFocus(ByVal sender As Object, ByVal e As RoutedEventArgs) e.Handled = True End Sub
I see what you are saying about the caret staying in the TextBox that looses focus. Try setting the IsEnabled property to False and then True again after you set e.Handled = True. That seems to make the caret go away for me.
Blog: http://windowsclientdevelopment.spaces.live.com FAQs: http://windowspresentationfoundation.wikispaces.com http://windowsmobile.wikispaces.com http://vsto.wikispaces.com