Results 1 to 21 of 21

Thread: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    13

    Resolved [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!

  2. #2
    Addicted Member thetimmer's Avatar
    Join Date
    Jan 2014
    Location
    Plano, Texas
    Posts
    243

    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    13

    Re: Select contents of textbox [VBA] both msOf07 and msOf10

    Quote Originally Posted by thetimmer View Post
    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.

  4. #4
    New Member
    Join Date
    Feb 2014
    Posts
    11

    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?

  5. #5
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    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.

  6. #6
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    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


  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    13

    Re: Select contents of textbox [VBA] both msOf07 and msOf10

    The suggestion in #5 solved the problems.

    Thanks everyone for the help!

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    13

    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?

  9. #9
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10

    Read my post #4...thought it was 4, guess it's 5

  10. #10
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    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


  11. #11
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    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)

  12. #12

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    13

    Re: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10

    Quote Originally Posted by vbfbryce View Post
    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?

  13. #13
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    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?

  14. #14

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    13

    Re: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10

    Quote Originally Posted by vbfbryce View Post
    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. :/

  15. #15

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    13

    Re: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10

    Quote Originally Posted by vbfbryce View Post
    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.

  16. #16
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    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?

  17. #17

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    13

    Re: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10

    Quote Originally Posted by vbfbryce View Post
    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.

  18. #18
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    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

  19. #19

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    13

    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

  20. #20

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    13

    Re: [RESOLVED] Select contents of textbox [VBA] both msOf07 and msOf10

    Quote Originally Posted by vbfbryce View Post
    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

  21. #21
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    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
  •  



Click Here to Expand Forum to Full Width