-
[RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10
I have a userform where I have different textboxes. What I want is for the all the contents to be selected upon focus on the box. I am trying to get this to work through Excel VBA.
Googling gave me some inspiration to use selstart and sellength.
The name of the box is txtStartHour, and the name of the sub is "txtStartHour_enter", I figure that "enter" is the VBA eqvivalent of "GotFocus" in VB6.
I have tried
With Me.txtStartHour
.SetFocus
.SelStart = 0
.SelLength = Len(Me.txtStartHour.Text)
End With
and
txtStartHour.setFocus
txtStartHour.SelStart=0
txtStartHour.SelLength=len(txtStartHour.text)
The sub is getting called, I am assured of this as when I go step by step the code get highlighted.
Thanks!
-
Re: Select contents of textbox [VBA] both msOf07 and msOf10
I think you're contending with the difference between giving the tb focus versus clicking in the box. This reply says that any time you enter a text box all text is selected except for when you click into it. When you click into it the cursor must get set at the point at which the click occurs. Try the text box click event to run your select code.
Also, I saw where someone suggests just doing TextBox1.SelLength = 9999 if you don't want to worry about your text length.
-
Re: Select contents of textbox [VBA] both msOf07 and msOf10
Quote:
Originally Posted by
thetimmer
I think you're contending with the difference between giving the tb focus versus clicking in the box.
This reply says that any time you enter a text box all text is selected except for when you click into it. When you click into it the cursor must get set at the point at which the click occurs. Try the text box click event to run your select code.
Also, I saw where someone suggests just doing TextBox1.SelLength = 9999 if you don't want to worry about your text length.
Thanks for the suggestions. I tried both of them, ie putting the code in the click-event (which btw isn't listed in the drop down menu). The click never got activated so it seems that it isnt the proper event routine for VBA. I did also try setting the length to 9999 but it didn't matter in either of them.
I'm kind of at a loss here, usually Google is my friend but today it has been a dead end.
-
Re: Select contents of textbox [VBA] both msOf07 and msOf10
I may be misunderstanding your requirements, but the following works perfectly for me:
Code:
Private Sub TextBox1_GotFocus()
TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1)
End Sub
Is there a particular reason why you want to use a separate Sub for this?
-
Re: Select contents of textbox [VBA] both msOf07 and msOf10
Not sure it makes good sense, but you can use mouse events, ie.:
Code:
Private Sub txt1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
txt1.SelStart = 0
txt1.SelLength = Len(txt1.Text)
End Sub
An obvious issue with this approach is that you couldn't click somewhere else in the text box without selecting all of its text again. You could do the selecting from the right click instead of the left, if that would help at all.
-
Re: Select contents of textbox [VBA] both msOf07 and msOf10
this works for me, i tested it in mouseup event
Code:
TextBox1.SelStart = 0
TextBox1.SelLength = TextBox1.TextLength
-
Re: Select contents of textbox [VBA] both msOf07 and msOf10
The suggestion in #5 solved the problems.
Thanks everyone for the help!
-
Re: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10
I noticed an issue with the solution using the mousebutton.
Lets say that the textbox contains "Hello World", I start to select "World" because I wish to delete just that part, now instead when I let go of the mousebutton to press delete the whole text is selected and everything is removed.
Any ideas on a workaround?
-
Re: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10
Read my post #4...thought it was 4, guess it's 5
-
Re: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10
i didnt test this but, using the mouse events you should be able get the position within the string when the action is performed so, to start use mouse down to set the SelStart position then using mouse up calculate the number of characters that has moved for the SelLength.
-
Re: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10
How do you WANT it to behave, in that case? You want it to select the whole word that you clicked on?
(asking the OP)
-
Re: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10
Quote:
Originally Posted by
vbfbryce
How do you WANT it to behave, in that case? You want it to select the whole word that you clicked on?
(asking the OP)
The way I see it there are two cases.
Case 1:
Another object has the focus, in that case I want all the text to be selected
Case 2:
The textbox already has the focus and just parts of it needs editing. User selects the parts of the text using the mouse. In this case I don't want to select all the text but rather do nothing.
Does that make sense? :)
-
Re: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10
In case 1, if another object has the focus, how do you know to select this text box?
-
Re: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10
Quote:
Originally Posted by
vbfbryce
Read my post #4...thought it was 4, guess it's 5
I did try this, ie using mouse down instead of up. It did not have the intended behavior. :/
-
Re: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10
Quote:
Originally Posted by
vbfbryce
In case 1, if another object has the focus, how do you know to select this text box?
It has to do with the user wanting to change the contents in that textbox. For example "Hello World", now he wants to remove "world" but only "world". He selects world but upon doing so with the mouse everything is selected instead with the current solution provided in #5 and #6.
-
Re: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10
I get that. My question is, what "event" tells your program to select all the text (in your case #1)? If the user doesn't click into the text box (sounds like case #2), then how do you know?
-
Re: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10
Quote:
Originally Posted by
vbfbryce
I get that. My question is, what "event" tells your program to select all the text (in your case #1)? If the user doesn't click into the text box (sounds like case #2), then how do you know?
Oh.
First I should clarify, another object has the focus prior to clicking in the box.
So in case #1 I am using mousebutton_Up for the textbox to trigger selecting all the content.
I have yet to try suggestio in post #10. Ill try it now.
-
Re: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10
Ok, hopefully that works. I'm still not clear on exactly what you want to happen, like:
1) If user tabs into textbox, do XYZ
2) If user clicks into textbox, do ABC
-
Re: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10
So I think it might have been solved.
Now I only select everything if the length of the selection is zero, which it will be when I click the box for the first time but not when I click the box to select parts of the text.
If Me.txtOutputSheet.SelLength = 0 Then
Me.txtOutputSheet.SelStart = 0
txtOutputSheet.SelLength = txtOutputSheet.TextLength
End If
-
Re: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10
Quote:
Originally Posted by
vbfbryce
Ok, hopefully that works. I'm still not clear on exactly what you want to happen, like:
1) If user tabs into textbox, do XYZ
2) If user clicks into textbox, do ABC
1) If a user tabs into textbox everything is selected by default.
2) If the user is clicking for the "first time" in the box everything should be highlighted, just like in 1). However if the user already has clicked in the box once and he only wishes to select parts of the text he should be able to do so. I'm sorry that I can't explain it much better than that.
Final code:
Private Sub txtOutputSheet_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Me.txtOutputSheet.SelLength = 0 Then
Me.txtOutputSheet.SelStart = 0
txtOutputSheet.SelLength = txtOutputSheet.TextLength
End If
End Sub
-
Re: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10
Gotcha. Looks like a winner!