+ Reply to Thread
Results 1 to 9 of 9

enter() event vs. mousedown() -- confusion

  1. #1
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    enter() event vs. mousedown() -- confusion

    Hi all. I have a lot of textboxes on a userform and I would like to Select All text in a textbox when a user enters a textbox. I can already do this when they 'tab' into a textbox, but when they click...I seem to be having problems selecting all text. Finally, I've gotten it to work by using this:

    Please Login or Register  to view this content.
    If I use the "Enter()" event, it won't work. Why is that? Everywhere I search on this forum and Google...I see people using "Enter()" event.

    Also, is there a way I can tell every textbox on my userform to do this? So everytime SetFocus is on a textbox, it should call a procedure that'll highlight all text? Just to make my code more organized and nicer I guess, instead of me going and copy-pasting this same above code into every textbox sub.

    Any help/insight into this would be very helpful. Thanks all!

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,486

    Re: enter() event vs. mousedown() -- confusion

    I am not sure what you mean, is there already text in the textbox?
    What are they clicking? Clicking into the textbox?

  3. #3
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: enter() event vs. mousedown() -- confusion

    yes, i put in sample text in the textbox (ex: "$0.00")

    and when i click into it (the texbox), id like it to select the whole $0.00 so that i can just override it with new text.

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: enter() event vs. mousedown() -- confusion

    Hi losmi8 - let me try an explanation.

    Your code above is Behind TextBox1 which lives on your Userform. If you press Enter while in this TextBox, the code isn't fired because it only fires when you do a MouseDown while in that TextBox.

    If you wanted something to happen when you pressed Enter anytime you were on the Userform, you'd have to put an On Enter() Event behind the Userform. Now that I look there is no Enter Event for a Userform. The events that are allowed for a control are on the right dropdown after selecting the object in the left dropdown above the VBA Code window. I don't see a Userform Close event so I search the list and see Deactivate. Press Help and read about Deactivate for a Userform. This has possibilities. You could save all text to Cells or Variables when the Userform Deactivates.

    Another way to solve this problem is to look at the Properties of Each TextBox. The property of ControlSource is the important one. If you set that TextBox to a Cell it will show that Cells Value in the TextBox and change that Value when you change the TextBoxes text.

    Simply put - Events only fire on the object you put the code behind.

    A good source is http://www.cpearson.com/excel/Events.aspx for events
    Another hint for learning. Click on the Textbox and then find ControlSource in the Properties of the TextBox window. Now Press F1 and see how it works. I see it can equal a String in the help file.

    I hope all this helps and isn't just redundant for what you have already discovered.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  5. #5
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: enter() event vs. mousedown() -- confusion

    Now that I did all that babble below. Look at the AutoWordSelect Property for the TextBox.

    Click AutoWordSelect in the Properties window and Press F1.

    I think this is what you want. All the stuff below may have missed the question.

  6. #6
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: enter() event vs. mousedown() -- confusion

    I don't think I've explained myself very well. All I am trying to accomplish is the following:

    When I click into a TextBox, I'd like whatever is in that TextBox to be Selected (as in Select All/Highlighted) so that when I hit a keystroke, everything that was in there will be replaced with this new keystroke.

    I don't care about hitting Enter.

    Also, is there way for me to do this for all of the TextBoxes by using "Set Focus" option? So if any TextBox is in focus, I call this sub somewhere that will highlight everything in that textbox?

    Marvin - all of my textboxes have that setting set to "True" and I don't see what it does when I turn it off. I'm not sure of its property.
    Last edited by losmi8; 02-11-2011 at 11:03 PM.

  7. #7
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,486

    Re: enter() event vs. mousedown() -- confusion

    See what this does for you
    Please Login or Register  to view this content.
    Click on TextBox2 and the entire text should be selected

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: enter() event vs. mousedown() -- confusion

    Hello losmi8,

    To answer the second part of the your question, there is no built in method you can apply to all the TextBoxes. However, it is possible to create a common event handler that is shared among all the TextBox controls.

    There are 3 parts to doing this. There are 2 Class modules. One module is for the TextBox event handling. The second creates a collection of individual event handlers and set references to each individual TextBox. Lastly, you need to add code to the form to add the TextBoxes to this collection.

    NOTE: Each Class Module must be separate. The code can not be combined into a single module.

    After adding the Class module, change the name in the Properties dialog to match the name in the header above the code. If you do not rename the modules, the code will not work.

    TextBoxEvents
    Please Login or Register  to view this content.


    TextBoxObjects
    Please Login or Register  to view this content.


    UserForm Code
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  9. #9
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: enter() event vs. mousedown() -- confusion

    Wow - Thanks guys! The mouseup works, but the mousedown seems to react a little faster and select all faster.

    Leith - I will try your code now and hopefully it will work! Thank you so much for such a long and detailed response!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1