-
Oct 18th, 2021, 01:19 PM
#1
Thread Starter
Fanatic Member
[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.
-
Oct 18th, 2021, 01:30 PM
#2
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
-
Oct 18th, 2021, 01:41 PM
#3
Re: Highlight Text on TextBox Enter
Any reason you've commented out the built-in SelectAll method?
-
Oct 18th, 2021, 02:36 PM
#4
Thread Starter
Fanatic Member
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.
-
Oct 18th, 2021, 03:00 PM
#5
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?
-
Oct 18th, 2021, 05:12 PM
#6
Thread Starter
Fanatic Member
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.
-
Oct 18th, 2021, 06:43 PM
#7
Thread Starter
Fanatic Member
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.
-
Oct 18th, 2021, 07:01 PM
#8
Thread Starter
Fanatic Member
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?
-
Oct 18th, 2021, 07:22 PM
#9
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 18th, 2021, 09:08 PM
#10
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.
-
Oct 18th, 2021, 09:21 PM
#11
Re: [RESOLVED] Highlight Text on TextBox Enter
Originally Posted by dday9
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.
-
Oct 18th, 2021, 09:23 PM
#12
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.
-
Oct 19th, 2021, 04:00 AM
#13
Thread Starter
Fanatic Member
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.
-
Oct 19th, 2021, 04:36 AM
#14
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:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Button1.TabIndex = 1
TextBox1.TabIndex = 2
Button2.TabIndex = 3
TextBox2.TabIndex = 4
Button3.TabIndex = 5
TextBox3.TabIndex = 6
TextBox1.Text = "First"
TextBox2.Text = "Second"
TextBox3.Text = "Third"
End Sub
Private Sub TextBoxes_Enter(sender As Object, e As EventArgs) Handles TextBox3.Enter, TextBox2.Enter, TextBox1.Enter
DirectCast(sender, TextBox).SelectAll()
End Sub
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.
-
Oct 19th, 2021, 05:12 AM
#15
Thread Starter
Fanatic Member
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
-
Oct 19th, 2021, 05:26 AM
#16
Re: [RESOLVED] Highlight Text on TextBox Enter
Originally Posted by sgrya1
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.
-
Oct 19th, 2021, 05:50 AM
#17
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|