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. Visual Basic
  4. Textbox keeps losing focus

Textbox keeps losing focus

Scheduled Pinned Locked Moved Visual Basic
debugginghelpquestion
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.
  • C Offline
    C Offline
    CJotaO
    wrote on last edited by
    #1

    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!

    D T 2 Replies Last reply
    0
    • C CJotaO

      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!

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • C CJotaO

        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!

        T Offline
        T Offline
        TnTinMn
        wrote on last edited by
        #3

        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 TextBox

        Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)
        SelectAll()
        MyBase.OnEnter(e)
        End Sub

        Private 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 Class

        Just 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.

        C 1 Reply Last reply
        0
        • T TnTinMn

          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 TextBox

          Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)
          SelectAll()
          MyBase.OnEnter(e)
          End Sub

          Private 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 Class

          Just 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.

          C Offline
          C Offline
          CJotaO
          wrote on last edited by
          #4

          Thanks TnTinMn! It Works fine. And thanks for helping me understand the "LostFocus Mystery" :laugh:

          T 1 Reply Last reply
          0
          • C CJotaO

            Thanks TnTinMn! It Works fine. And thanks for helping me understand the "LostFocus Mystery" :laugh:

            T Offline
            T Offline
            TnTinMn
            wrote on last edited by
            #5

            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.

            C 1 Reply Last reply
            0
            • T TnTinMn

              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.

              C Offline
              C Offline
              CJotaO
              wrote on last edited by
              #6

              I will. I know now the way. It's easier to explore. :)

              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