Results 1 to 13 of 13

Thread: [RESOLVED] [2.0] Selecting textbox text upon gaining focus

  1. #1

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Resolved [RESOLVED] [2.0] Selecting textbox text upon gaining focus

    Hi all,

    I am trying to select all text in a textbox when it gains focus, either by keyboard or mouse - as is common in UI's. I have tried using the GotFocus and Enter events but neither of them worked when clicking in the textbox. Seems they fire too early and the selection is overriden by the mouse click.

    Here is the code I used
    Code:
    void textbox_Enter(object sender, EventArgs e)
    {
      ((TextBox)sender).SelectAll();
    }
    As I said above it works with keyboard but not with mouse. So can anyone advise me of a decent method to achieve this effect both ways?

    Thanks
    - P

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2.0] Selecting textbox text upon gaining focus

    Place the code in the MouseUp event too.

    So what your after is a textbox that continuously selected no matter the action?

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2.0] Selecting textbox text upon gaining focus

    I found this description of events that should help.
    When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl.ActiveControl property to the current form, focus events occur in the following order:

    Enter
    GotFocus
    Leave
    Validating
    Validated
    LostFocus


    When you change the focus by using the mouse or by calling the Focus method, focus events occur in the following order:

    Enter
    GotFocus
    LostFocus
    Leave
    Validating
    Validated
    If the CausesValidation property is set to false, the Validating and Validated events are suppressed.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] Selecting textbox text upon gaining focus

    It fires - it's just that if I use the mouse, the selection doesn't happen.

    I don't want it permanently selected. Just when it gains focus.


    Edit: Works in MouseUp. I would simply rather not have to trap two events, but oh well.

    Thanks.
    Last edited by penagate; Jun 1st, 2006 at 12:52 AM.

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2.0] Selecting textbox text upon gaining focus

    Just tested on an open VB.net project and this works for the mouse.
    VB Code:
    1. Private Sub txtAddress1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txtAddress1.MouseUp
    2.     Me.txtAddress1.SelectAll()
    3. End Sub

    Edit: Just 1 minute too late.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2.0] Selecting textbox text upon gaining focus

    This is actually more complex than you think. If you handle the MouseUp, Click or MouseClick event and call SelectAll there, what happens if the control already has focus and the user clicks it to place the caret? Bam! SelectAll.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] [2.0] Selecting textbox text upon gaining focus

    Feck.

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] [2.0] Selecting textbox text upon gaining focus

    So then what, use a boolean flag in the Enter event to signify that its the first time selecting the text. Then if they are using the mouse to re-focus you bypass selecting the text. Probably something along those lines.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2.0] Selecting textbox text upon gaining focus

    Indeed. Use the same event handlers for all the TextBoxes you want to behave this way:
    Code:
    private bool selectAll = true;
    
    private void TextBox_Enter(object sender, EventArgs e)
    {
        ((TextBox)sender).SelectAll();
    }
    
    private void TextBox_MouseClick(object sender, MouseEventArgs e)
    {
        if (this.selectAll)
        {
            ((TextBox)sender).SelectAll();
            this.selectAll = false;
        }
    }
    
    private void TextBox_Leave(object sender, EventArgs e)
    {
        this.selectAll = true;
    }
    Last edited by jmcilhinney; Jun 1st, 2006 at 02:34 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2.0] Selecting textbox text upon gaining focus

    Actually that's imperfect too. If you tab in and then move the caret with the keyboard then click you will reselect everything. You'd probably have to add a KeyPress event handler as well and set selectAll to False.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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

    Re: [RESOLVED] [2.0] Selecting textbox text upon gaining focus

    KeyPress isn't raised by the arrow keys so I think adding this should sort you out:
    Code:
    private void TextBox_KeyDown(object sender, KeyEventArgs e)
    {
        this.selectAll = false;
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] [2.0] Selecting textbox text upon gaining focus

    Thanks. I'll try that all out tommorow.

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2.0] Selecting textbox text upon gaining focus

    Quote Originally Posted by penagate
    Thanks. I'll try that all out tommorow.
    Now, d*mn it. Now. I'm still working in Sydney and it's earlier in Adelaide. Slacker.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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