Has anyone figured out a way to filter textbox input? For example, I wish to have the textbox ignore any key press that is not numeric. I tried the IsNumber function in the KeyPress event but that doesn't work.
The first approach I tried was:
Sub TextBox1_KeyPress (key)
if IsNumeric(key) = False then key = ""
End Sub
But changing the variable "key" within that subroutine does not do anything to what is displayed in the textbox. To illustrate this, replace the "if" statement above with "key = R". The textbox will still display what you type, not the value "R".
My second approach was to chop the right-hand character off of TextBox1.Text but alas, TextBox1.Text is not updated until AFTER the TextBox1.KeyPress(key) subroutine has been completed.
The only other events for a textbox are GotFocus and LostFocus, neither of which helps for this problem. The KeyPress event allows me to detect which key was pressed, but does not let me do anything about it.
Any ideas?
-dlfallen
The first approach I tried was:
Sub TextBox1_KeyPress (key)
if IsNumeric(key) = False then key = ""
End Sub
But changing the variable "key" within that subroutine does not do anything to what is displayed in the textbox. To illustrate this, replace the "if" statement above with "key = R". The textbox will still display what you type, not the value "R".
My second approach was to chop the right-hand character off of TextBox1.Text but alas, TextBox1.Text is not updated until AFTER the TextBox1.KeyPress(key) subroutine has been completed.
The only other events for a textbox are GotFocus and LostFocus, neither of which helps for this problem. The KeyPress event allows me to detect which key was pressed, but does not let me do anything about it.
Any ideas?
-dlfallen