Numeric/Uppercase Only TextBox w/ Undo via Subclassing

Using the GetWindowLong and SetWindowLong API functions you can change the style attributes of a standard textbox to only allow the values 0 to 9 or uppercase letters. This prevents you from having to validate the textbox's contents or convert to uppercase in the KeyPress event.

You can also easily add Undo capabilities to your textbox using the SendMessage API. Using this method there is no need to store the old text or worry about detecting if it changed or not.

For further discussion on how to change the behaviour of Visual Basic's intrinsic controls, see my Change a Control's Style / Owner Drawn Controls page.

Accept only numeric or uppercase values. Add Undo capability
Download Source Code

Numeric Only / Upper Case Only Textbox

Instead of checking for numeric values or converting ower case to upper case in the KeyPress event, you can start out with a textbox that only allows the values that you want. You do this by setting the Style attributes of the text box. First, get the existing style by calling the GetWindowLong API with the GWL_STYLE flag. Then set the new style and call SetWindowLong to put your changes into effect. This must be done in your form load event.

   l = GetWindowLong(Text1.hwnd, GWL_STYLE)
   r = SetWindowLong(Text1.hwnd, GWL_STYLE, l Or ES_NUMBER)
  
   l = GetWindowLong(Text2.hwnd, GWL_STYLE)
   r = SetWindowLong(Text2.hwnd, GWL_STYLE, l Or ES_UPPERCASE)

Undo Capability

You can provide undo capability to your textbox controls without the need to remember what the prior values was and if it was changed or not. By sending a few messages to the textbox you can harness its build in ability to undo changes.

When a value is entered and the "Save" button is pressed a message is sent to the textbox to empty its build in undo buffer and then mark its text as being changed. Now to undo changes just click the "Undo" button. This sends a message asking if the change can be undone. If the answer is yes, another message tells it to retrieve the old text from its undo buffer and display it.

Instructions

Download the program and press F5 to run it. Try typing anything other than a numeric value into the top textbox to verify it will only accept numbers. Note that anything typed into the middle textbox is converted to uppercase.

Enter text into the bottom textbox and click "Undo" to restore the previous value. Click "Save" to save the current text to the undo buffer. Now when you click "Undo" the saved text will be displayed. When you press "Can Undo?" a message indicates whether the changes can be undone or not. If the text was changed, a "Yes" is displayed. After clicking the "Save" button, a "No" is displayed. To enable the undo feature, click the "Mark" button which marks the text as being changed.




About TheScarms
About TheScarms


Sample code
version info

If you use this code, please mention "www.TheScarms.com"

Email this page


© Copyright 2024 TheScarms
Goto top of page