|
-
Jun 1st, 2006, 12:31 AM
#1
[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
-
Jun 1st, 2006, 12:41 AM
#2
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jun 1st, 2006, 12:45 AM
#3
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jun 1st, 2006, 12:47 AM
#4
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.
-
Jun 1st, 2006, 12:51 AM
#5
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:
Private Sub txtAddress1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txtAddress1.MouseUp
Me.txtAddress1.SelectAll()
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jun 1st, 2006, 01:43 AM
#6
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.
-
Jun 1st, 2006, 02:08 AM
#7
Re: [RESOLVED] [2.0] Selecting textbox text upon gaining focus
-
Jun 1st, 2006, 02:14 AM
#8
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jun 1st, 2006, 02:30 AM
#9
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.
-
Jun 1st, 2006, 02:37 AM
#10
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.
-
Jun 1st, 2006, 02:42 AM
#11
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;
}
-
Jun 1st, 2006, 04:57 AM
#12
Re: [RESOLVED] [2.0] Selecting textbox text upon gaining focus
Thanks. I'll try that all out tommorow.
-
Jun 1st, 2006, 05:17 AM
#13
Re: [RESOLVED] [2.0] Selecting textbox text upon gaining focus
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|