|
-
Feb 27th, 2014, 08:35 AM
#1
Thread Starter
New Member
[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!
-
Feb 27th, 2014, 08:45 AM
#2
Addicted Member
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.
-
Feb 27th, 2014, 09:22 AM
#3
Thread Starter
New Member
Re: Select contents of textbox [VBA] both msOf07 and msOf10
 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.
-
Feb 27th, 2014, 10:01 AM
#4
New Member
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?
-
Feb 27th, 2014, 10:24 AM
#5
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.
-
Feb 27th, 2014, 10:29 AM
#6
Fanatic Member
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
Yes!!!
Working from home is so much better than working in an office...
Nothing can beat the combined stress of getting your work done on time whilst
1. one toddler keeps pressing your AVR's power button
2. one baby keeps crying for milk
3. one child keeps running in and out of the house screaming and shouting
4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
5. working at 1 O'clock in the morning because nobody is awake at that time
6. being grossly underpaid for all your hard work

-
Feb 27th, 2014, 10:37 AM
#7
Thread Starter
New Member
Re: Select contents of textbox [VBA] both msOf07 and msOf10
The suggestion in #5 solved the problems.
Thanks everyone for the help!
-
Feb 27th, 2014, 11:01 AM
#8
Thread Starter
New Member
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?
-
Feb 27th, 2014, 11:21 AM
#9
Re: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10
Read my post #4...thought it was 4, guess it's 5
-
Feb 27th, 2014, 11:31 AM
#10
Fanatic Member
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.
Yes!!!
Working from home is so much better than working in an office...
Nothing can beat the combined stress of getting your work done on time whilst
1. one toddler keeps pressing your AVR's power button
2. one baby keeps crying for milk
3. one child keeps running in and out of the house screaming and shouting
4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
5. working at 1 O'clock in the morning because nobody is awake at that time
6. being grossly underpaid for all your hard work

-
Feb 27th, 2014, 11:49 AM
#11
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)
-
Feb 27th, 2014, 12:02 PM
#12
Thread Starter
New Member
Re: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10
 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?
-
Feb 27th, 2014, 12:05 PM
#13
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?
-
Feb 27th, 2014, 12:05 PM
#14
Thread Starter
New Member
Re: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10
 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. :/
-
Feb 27th, 2014, 12:09 PM
#15
Thread Starter
New Member
Re: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10
 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.
-
Feb 27th, 2014, 12:12 PM
#16
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?
-
Feb 27th, 2014, 12:17 PM
#17
Thread Starter
New Member
Re: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10
 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.
-
Feb 27th, 2014, 12:23 PM
#18
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
-
Feb 27th, 2014, 12:23 PM
#19
Thread Starter
New Member
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
-
Feb 27th, 2014, 12:26 PM
#20
Thread Starter
New Member
Re: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10
 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
-
Feb 27th, 2014, 12:47 PM
#21
Re: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10
Gotcha. Looks like a winner!
Tags for this Thread
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
|