Results 1 to 17 of 17

Thread: [RESOLVED] Highlight Text on TextBox Enter

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    Resolved [RESOLVED] Highlight Text on TextBox Enter

    I'm certain this was working at some stage. Wondering why it isn't now.

    HTML Code:
        Private Sub EnterTextBox(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNoBars.Enter, txtMultiplier.Enter, txtCenters.Enter
            'DirectCast(sender, System.Windows.Forms.TextBox).SelectAll() 'On entering the textbox select all text
            Dim myTextBox = DirectCast(sender, System.Windows.Forms.TextBox)
            myTextBox.SelectionStart = 0
            myTextBox.SelectionLength = myTextBox.Text.Length
        End Sub
    I've stepped through the code and there's nothing past this.
    Last edited by sgrya1; Oct 19th, 2021 at 03:54 AM.

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Highlight Text on TextBox Enter

    So that "Handles txtNoBars.Enter, txtMultiplier.Enter, txtCenters.Enter".
    Is it those textboxes that aren't working, or another one that should be added to the Handles clause?
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Highlight Text on TextBox Enter

    Any reason you've commented out the built-in SelectAll method?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    Re: Highlight Text on TextBox Enter

    I get a breakpoint on the line so it is being handled.
    I commented out the .SelectAll because was that that wasn't working either.

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Highlight Text on TextBox Enter

    Per the MSDN documentation, the GotFocus event would fire immediately after the Enter event. Is it possible that you have code overwriting the code shown in the Enter event?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    Re: Highlight Text on TextBox Enter

    Maybe it's a bug with the application I'm using it under (AutoCAD).
    I created a new textbox and the same problem exists.
    Wierd, it used to work.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    Re: Highlight Text on TextBox Enter

    I've realised that Enter is not the right event for the selection of text, for some reason.
    Needs to be Click or something else.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    Re: [RESOLVED] Highlight Text on TextBox Enter

    Dammit.
    _Enter doesn't select the text.
    _Click doesn't allow me to select part of the text.
    Is there something inbetween?

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: [RESOLVED] Highlight Text on TextBox Enter

    Show us the handles clause on the _Enter handler, and list the textboxes you want it to work with...

  10. #10
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: [RESOLVED] Highlight Text on TextBox Enter

    There is the GotFocus event (as mentioned earlier). Per the documentation:
    Occurs when the control receives focus.
    This gets fired after the Enter event.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: [RESOLVED] Highlight Text on TextBox Enter

    Quote Originally Posted by dday9 View Post
    There is the GotFocus event (as mentioned earlier). Per the documentation:


    This gets fired after the Enter event.
    That should generally not be used by application developers but, as this appears to be an AutoCAD plugin, that rule may not apply.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: [RESOLVED] Highlight Text on TextBox Enter

    Is the issue resolved or not? You marked the thread Resolved but then posted again as though the issue persists. If the issue is resolved then maybe you could provide the resolution, which could help others. If it's not resolved, please edit the thread title to remove the indication that it is. You should be able to do that with your post count.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    Re: Highlight Text on TextBox Enter

    Thanks. I didn't realise I could do that.
    And sorry, it's not problem related to AutoCAD. I should have tested this with a fresh VS project before suggesting that.

    I want to highlight the text on entering it (or the control getting focus) but not thereafter when selecting a portion of the text.

    Neither TextBox1.Enter or TextBox1.GotFocus wants to highlight the text.
    And TextBox1.Click highlights all the text, even when I select part of the text, which does make sense.

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Highlight Text on TextBox Enter

    I suspect that the code is working exactly as it should. Try this. Create a new WinForms App project, add three Buttons and three TextBoxes to the form, then add the following code:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    4.         Button1.TabIndex = 1
    5.         TextBox1.TabIndex = 2
    6.         Button2.TabIndex = 3
    7.         TextBox2.TabIndex = 4
    8.         Button3.TabIndex = 5
    9.         TextBox3.TabIndex = 6
    10.  
    11.         TextBox1.Text = "First"
    12.         TextBox2.Text = "Second"
    13.         TextBox3.Text = "Third"
    14.     End Sub
    15.  
    16.     Private Sub TextBoxes_Enter(sender As Object, e As EventArgs) Handles TextBox3.Enter, TextBox2.Enter, TextBox1.Enter
    17.         DirectCast(sender, TextBox).SelectAll()
    18.     End Sub
    19.  
    20. End Class
    Now run the project and press the Tab key repeatedly. Is the full text selected each time? When a TextBox has focus, click somewhere in the text and then Tab out and back into the TextBox. Is the full text selected again? I'm quite sure that it will be and that proves that the code is working properly.

    What I'm guessing is happening is that you are clicking a Textbox to give it focus and you're assuming that that should select all the text but of course it won't. That's because the Enter event is raised first, which selects all the text, and then a Click event is raised, which positions the caret where you clicked. By clicking, you are undoing the selection. You can see that this is obviously the case because clicking an unfocused TextBox will place the caret where you clicked.

    If what you want is to be able to click a TextBox that doesn't have focus and have all the text selected then there's no simple way to do so. If you handle every Click or MouseClick then you'll always be selecting all the text on every click. If you try to detect the first click after receiving focus then the user could Tab in and then click some time later, so that wouldn't really work. The best option would probably be to create a custom control that inherits TextBox and filters Windows message to determine the reason that its receiving focus and then select all text or not on the next Click based on that. If you want to use a standard TextBox then I think your best option might be to handle Enter and record the time and the TextBox, then handle Click and check for the same TextBox and whether it is within an appropriate period of time, e.g. 100 ms, of the Enter event and select all the text if it is.

    The important takeaway here is to think about what events are going to be raised in any particular scenario, rather than just thinking about the event(s) you care about and ignoring all others.

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    Re: [RESOLVED] Highlight Text on TextBox Enter

    Thank you all.
    I basically want the textbox to behave as a number cell. Like Excel or a datagridview.
    But a textbox is designed for text.
    I think I figured out a workaround.

    HTML Code:
        Dim EnteringTxtBox As Boolean
        Private Sub TextBoxes_Enter(sender As Object, e As EventArgs) Handles TextBox3.Enter, TextBox2.Enter, TextBox1.Enter
            EnteringTxtBox = True
        End Sub
        Private Sub TextBox1_Click(sender As Object, e As EventArgs) Handles TextBox1.Click, TextBox2.Click, TextBox3.Click
            If EnteringTxtBox Then
                DirectCast(sender, TextBox).SelectAll()
                EnteringTxtBox = False
            End If
        End Sub

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: [RESOLVED] Highlight Text on TextBox Enter

    Quote Originally Posted by sgrya1 View Post
    Thank you all.
    I basically want the textbox to behave as a number cell. Like Excel or a datagridview.
    But a textbox is designed for text.
    I think I figured out a workaround.

    HTML Code:
        Dim EnteringTxtBox As Boolean
        Private Sub TextBoxes_Enter(sender As Object, e As EventArgs) Handles TextBox3.Enter, TextBox2.Enter, TextBox1.Enter
            EnteringTxtBox = True
        End Sub
        Private Sub TextBox1_Click(sender As Object, e As EventArgs) Handles TextBox1.Click, TextBox2.Click, TextBox3.Click
            If EnteringTxtBox Then
                DirectCast(sender, TextBox).SelectAll()
                EnteringTxtBox = False
            End If
        End Sub
    As I mentioned in my previous post, that will behave as you want if you click into a TextBox but if you Tab in and then click to position the caret, it will select all the text on that click. You need to remember that a lot of people use the keyboard to navigate forms, especially ones that are heavily into data entry. It's far more efficient to keep your hands on the keyboard than keep grabbing the mouse all the time.

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    Re: [RESOLVED] Highlight Text on TextBox Enter

    My app is a plugin to a program that is primrarily driven using a mouse.
    I used a custom 'NumberBox' control that someone had created on a previous Visual Basic project.
    I remember it being quite powerful with many features.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width