|
|
How to Create a Numeric Only TextBox Control | | Have you ever needed a Windows.Forms.TextBox control that only accepted letters, or that only accepted numbers? Well, the other day, I needed one and after some playing around, I decided that it's something I may well need in the future. Taking the simple logic contained below, you can easily subclass the Winows.Forms.TextBox control and stick it in your toolbox for later use. <Note that for the sake of illustration, I created two extra Booleans to use in the evaluation...you could save yourself two lines of code by including them directly>
Private Overloads Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim isKey As Boolean = e.KeyChar.IsDigit(e.KeyChar)
Dim isDecimal As Boolean = e.KeyChar.ToString = "."
If Not isKey And Not isDecimal Then
e.Handled = True
End If
End Sub
| The use above will allow all valid numbers as well as decimals. It wouldn't be very difficult to expand upon this to verify that you don't have two decimal in the number, allow for currency characters etc.
To flip this around, if you wanted to allow only Letters in your text box... the following should do it for you:
Private Overloads Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim isKey As Boolean = e.KeyChar.IsDigit(e.KeyChar)
If isKey Then
e.Handled = True
End If
End Sub
| Now, you could easily combine these types of methods with some more sophisticated methodology, allowing for overloads for instance, to create a textbox that validates the input and changes color according to certain specifications:
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
If Not IsNumeric(TextBox2.Text) Then Exit Sub
If CType(TextBox2.Text, Int16) < 20 Then
TextBox2.BackColor = Color.Red
Else
TextBox2.BackColor = Color.WhiteSmoke
End If
End Sub |
Similarly, you could use the GotFocus and LostFocus events to change the background colors slightly, to give the user some feedback that they are in or out of the control. The nice part about this is that you can easily create a custom text box, or you can just add the code to one text box and add multiple other textbox controls to it's event handler (See my article on Event Handling for a more in depth look at using Handlers)
Private Sub tbFirstName_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles tbFirstName.GotFocus, tbLastName.GotFocus, tbPhysician.GotFocus, tbSSN.GotFocus
CType(sender, TextBox).BackColor = Color.LightGray
End Sub
Private Sub tbFirstName_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles tbFirstName.LostFocus, tbLastName.LostFocus, tbPhysician.LostFocus, tbSSN.LostFocus
CType(sender, TextBox).BackColor = Color.White
End Sub
| Note that in the above example, I'm strongly typing the evaluation. In this case, we know what they type of Sender is, so this isn't really necessary other than explicitly showing my intentions, providing intellisense and being slightly more efficient code wise, but if you are handling multiple objects, there will definitely be instances where you want to check the type of object and respond accordingly.
These are just some very generic examples of the cool stuff you can do with .NET Controls, and hopefully you can come up with many ideas on your own to save time and provide a richer UI experience for your users.
|
|