My Anna

Indias Biggest Engineering Education Community Enriching Students Minds with Latest Placement Papers, Study Materials, Fresher Jobs and Free E Books

A Unit of Aim Clear Technologies Ltd.
Search Study Materials, Jobs, Placement Papers, Interview Tips, Training Videos & More

Advertise with us

Who is online



Post new topic Reply to topic  [ 1 post ] 

sreenath
Super Moderator
Super Moderator
User avatar

Joined:
Wed Aug 26, 2009 10:01 pm
Beans: 1,227
In Bank: 14665
College: ESEC
Degree: M.Sc
Department: CSE
State: TN
Fav Quote: no pain no gain
 
#2
Sat Oct 03, 2009 2:58 pm
Post subject: How to Create a Numeric Only TextBox Control Post
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
_________________
sreenath@myanna.inImage


Profile E-mail
Offline

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 
Bookmark & Share

Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB Group.

phpBB SEO