+ Reply to Thread
Results 1 to 12 of 12

Userform textbox, account for Backspace and Delete

  1. #1
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Userform textbox, account for Backspace and Delete

    This userform edits cell contents as you type, while preserving the font.

    The initiation starts at the class module "clsTextBox". There are two methods. (1) "MyTextBox_Change" is used when the cursor is placed somewhere before typing begins. (2) "MyTextBox_KeyPress" is used if part of the text is highlighted before typing.

    There is an issue if the BACKSPACE or DELETE key is used, it throws out the fonts.

    Is there a way to capture the BACKSPACE and DELETE event? So I can write a separate module for this.



    CLASS MODULE clsTextBox
    Please Login or Register  to view this content.

    FORM CODE
    Please Login or Register  to view this content.
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Userform textbox, account for Backspace and Delete

    Trap it with the KeyDown event

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: Userform textbox, account for Backspace and Delete

    This is creating an event, which is a good step forward.

  4. #4
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: Userform textbox, account for Backspace and Delete

    Is it possible to relay the "character typed" information to the Sub in the form code?

  5. #5
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: Userform textbox, account for Backspace and Delete

    I've tried to delete a selected portion of text with "KeyDown". It doesn't work.
    There seems to be a lag of one event. Pressing the second time shows the result for the first time, pressing for the third time shows the result for the second time, and so on.
    When I say it doesn't work, the selected string characters are not deleted, but the fonts are changed though.

    Please Login or Register  to view this content.
    Attached Files Attached Files

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Userform textbox, account for Backspace and Delete

    With the KeyDown event, the textbox is not updated until after the event. So your transfertwoless procedure is not evaluating the most recent key press. It's evaluating the existing text that has not updated yet. I think you want you pass the KeyCode as an argument to transfertwoless and process the Backspace-Delete conditions. Alternatively, consider using the KeyUp event which the Textbox update before that event.

  7. #7
    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: Userform textbox, account for Backspace and Delete

    Hello Un-Do Re-Do,

    To prevent the KeyCode from being processed, set it to zero. Sub-Classing is a FIFO stack oriented process. What does not get processed by your class will be passed along to the original class.

    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!)

  8. #8
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: Userform textbox, account for Backspace and Delete

    I've tried the suggestion at post #6 by passing the argument into the Sub (with "KeyDown"). I can't make it update in the Sub though.
    I did try "KeyUp" yesterday but this does not store the value of the SelectionCount, the number of selected characters. This was one of the other requirements which allowed the ability to type over a selected number of characters.
    "KeyPress" (when combined with MyTextBox_Change()) did update and restore fonts but unfortunately does not handle Delete.
    A summary is shown below.

    With the suggestion at post #7, the intention is to actually to implement the BackSpace and Delete, not prevent it.

    See attached workbook for latest working.

    Attachment 643760
    Attached Files Attached Files

  9. #9
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: Userform textbox, account for Backspace and Delete

    I've progressed this somewhat over a lengthy period.
    It's getting closer but still baffles.

    First, to explain the theme:
    The order of events is "KeyDown", "Change" of textbox, then "KeyUp". Public declarations are used to relay information between the three.

    KeyDown is required to capture the instance where a selection is made (highlight a number of characters and type over them). "Change" of textbox is used to transfer contents to cell, from which a new array of characters is manipulated. "KeyUp" is required since it handles Backspace and Delete and captures the event after the textbox change. This took me days to figure out ... using Debug.Print window.

    The code still does not work in instances such as Backspace and Delete. I can understand the details of what's happening however it still baffles me why the fonts are not restored correctly. The algorithm formulas look correct.

    Any further suggestions?

    CLASS MODULE (Part)
    Please Login or Register  to view this content.
    FORM CODE (Part)
    Please Login or Register  to view this content.
    Attached Files Attached Files

  10. #10
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Userform textbox, account for Backspace and Delete

    To easily associate a textbox with a cell, I put the cell address in the Textbox name. I then later extract the cell address from the name to reference the cell.

    Please Login or Register  to view this content.
    This seems to work though I didn't test it a lot.
    clsTextBox

    Please Login or Register  to view this content.
    Last edited by AlphaFrog; 10-05-2019 at 08:06 AM.

  11. #11
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: Userform textbox, account for Backspace and Delete

    @AlphaFrog
    The code is much simpler and intuitive. I've tested with no selection (cursor only) and this works. It will need a tweak when there is a selection in the text box. It also accounts for Navigation keys (non text), which was another part I had to add to stop errors.
    Thanks for assisting.

    Incidentally with the code at post #9, the error for Backspace or Delete was due to the following:
    "SelectionCount" should have been "SelectionCountHold"

  12. #12
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Userform textbox, account for Backspace and Delete

    Here's a better version FWIW

    Please Login or Register  to view this content.
    Last edited by AlphaFrog; 10-06-2019 at 05:58 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Masked Control Textbox entry field - backspace not working right
    By Arnold Layne in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-28-2019, 07:34 PM
  2. [SOLVED] How to allow TextBox backspace key?
    By VAer in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-08-2017, 09:34 AM
  3. [SOLVED] How to clear all text form textbox with one backspace (userform)
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-16-2017, 12:36 PM
  4. [SOLVED] Narrowing Results in ListBox using TextBox that Reloads when Backspace is Struck
    By kak132 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-24-2014, 12:10 PM
  5. [SOLVED] Userform: Backspace button and Shift key
    By Gal403 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-08-2014, 03:25 AM
  6. Enable backspace key in combobox on Userform
    By bdb1974 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-06-2011, 05:35 PM
  7. Assigning backspace and delete to form buttons
    By solnajeff in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-03-2010, 09:32 AM

Tags for this Thread

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