Textbox keeps losing focus
-
I want to select all text in a textbox, so I coded the SelectAll() method inside the GotFocus event handler. This did not work, so I inserted a breakpoint to try to find the problem and what I found is that after the execution of the SelectAll() instruction the LostFocus event is fired and the code within the LostFocus handler executed. The same thing hapens when using the TAB key to go to the textbox. However in this case the text remains selected Bottom line I want to select the all text in the text box after clicking the control. I looked around and the MouseDown event does the trick. However if I try to move the insertion point by clicking with the mouse all the text is selected again. And that is not a desirable behavior for this program. Any ideas? Thanks In Advance!
-
I want to select all text in a textbox, so I coded the SelectAll() method inside the GotFocus event handler. This did not work, so I inserted a breakpoint to try to find the problem and what I found is that after the execution of the SelectAll() instruction the LostFocus event is fired and the code within the LostFocus handler executed. The same thing hapens when using the TAB key to go to the textbox. However in this case the text remains selected Bottom line I want to select the all text in the text box after clicking the control. I looked around and the MouseDown event does the trick. However if I try to move the insertion point by clicking with the mouse all the text is selected again. And that is not a desirable behavior for this program. Any ideas? Thanks In Advance!
Without seeing your code for these two event handles, it's impossible to tell you what you did wrong. Either your code is wrong or your testing method and/or description of the problem is. SelectAll will NOT change the focus to another control or remove focus from the control that you called SelectAll on.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
I want to select all text in a textbox, so I coded the SelectAll() method inside the GotFocus event handler. This did not work, so I inserted a breakpoint to try to find the problem and what I found is that after the execution of the SelectAll() instruction the LostFocus event is fired and the code within the LostFocus handler executed. The same thing hapens when using the TAB key to go to the textbox. However in this case the text remains selected Bottom line I want to select the all text in the text box after clicking the control. I looked around and the MouseDown event does the trick. However if I try to move the insertion point by clicking with the mouse all the text is selected again. And that is not a desirable behavior for this program. Any ideas? Thanks In Advance!
Quote:
I inserted a breakpoint to try to find the problem and what I found is that after the execution of the SelectAll() instruction the LostFocus event is fired
You observed the LostFocus event after hitting your breakpoint in GotFocus because the breakpoint transfered the focus to the debugger. Ain't event debugging fun? ;P I believe that you will need a custom control to change the default handling mouse handling to get the effect that you want. Here is quick example that intercepts the left mouse button down message to achieve this behavior.
Public Class TBSelectOnEnter
Inherits TextBoxProtected Overrides Sub OnEnter(ByVal e As System.EventArgs)
SelectAll()
MyBase.OnEnter(e)
End SubPrivate Const WM_LBUTTONDOWN As Int32 = &H201
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
' only intercept WM_LBUTTONDOWN if not focused to allow caret positioning with subsequent clicks
If Not Focused AndAlso (m.Msg = WM_LBUTTONDOWN) Then
'grab focus, abort message processing
Me.Focus()
Else
MyBase.WndProc(m)
End If
End Sub
End ClassJust add this to your code and do a build. It should show up in your toolbox at the top in the "YourApplicationName Components" section. Then use it as your TextBox.
-
Quote:
I inserted a breakpoint to try to find the problem and what I found is that after the execution of the SelectAll() instruction the LostFocus event is fired
You observed the LostFocus event after hitting your breakpoint in GotFocus because the breakpoint transfered the focus to the debugger. Ain't event debugging fun? ;P I believe that you will need a custom control to change the default handling mouse handling to get the effect that you want. Here is quick example that intercepts the left mouse button down message to achieve this behavior.
Public Class TBSelectOnEnter
Inherits TextBoxProtected Overrides Sub OnEnter(ByVal e As System.EventArgs)
SelectAll()
MyBase.OnEnter(e)
End SubPrivate Const WM_LBUTTONDOWN As Int32 = &H201
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
' only intercept WM_LBUTTONDOWN if not focused to allow caret positioning with subsequent clicks
If Not Focused AndAlso (m.Msg = WM_LBUTTONDOWN) Then
'grab focus, abort message processing
Me.Focus()
Else
MyBase.WndProc(m)
End If
End Sub
End ClassJust add this to your code and do a build. It should show up in your toolbox at the top in the "YourApplicationName Components" section. Then use it as your TextBox.
-
Thanks TnTinMn! It Works fine. And thanks for helping me understand the "LostFocus Mystery" :laugh:
-
You are welcome. Just be careful with that control. I did not put a lot of thought into the ramifications of "eating" the mouse down message, but I can not think of any.