473,385 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Textbox Validation for Two Decimal Places

7
Hi All,
I need to amend the code below to cater for only two Decimal places. Anyone know how i can do this?

Expand|Select|Wrap|Line Numbers
  1.  Private Sub TextboxKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress, TextBox7.KeyPress
  2.  
  3.         Dim tb As TextBox = CType(sender, TextBox)
  4.         Dim chr As Char = e.KeyChar
  5.  
  6.         If IsNumeric(e.KeyChar) And Not e.KeyChar = "-" Then
  7.             e.Handled = Not IsNumeric(tb.Text & e.KeyChar)
  8.         ElseIf e.KeyChar = "." Then
  9.             If Not (tb.SelectedText = "." Or IsNumeric(tb.Text & e.KeyChar)) Then
  10.                 e.Handled = True
  11.             End If
  12.         ElseIf Not Char.IsControl(e.KeyChar) Then
  13.             e.Handled = True
  14.         End If
  15.     End Sub 
  16.  

Thanks
Mar 2 '10 #1
3 8804
!NoItAll
297 100+
Put one additional test in there...

Expand|Select|Wrap|Line Numbers
  1.     Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
  2.         Dim tb As TextBox = CType(sender, TextBox)
  3.         Dim chr As Char = e.KeyChar
  4.         Dim iLoc As Integer = 0
  5.  
  6.         If IsNumeric(e.KeyChar) And Not e.KeyChar = "-" Then
  7.             e.Handled = Not IsNumeric(tb.Text & e.KeyChar)
  8.         ElseIf e.KeyChar = "." Then
  9.             If Not (tb.SelectedText = "." Or IsNumeric(tb.Text & e.KeyChar)) Then
  10.                 e.Handled = True
  11.             End If
  12.         ElseIf Not Char.IsControl(e.KeyChar) Then
  13.             e.Handled = True
  14.         End If
  15.  
  16.         iLoc = TextBox1.Text.IndexOf(".")
  17.         If iLoc > 0 Then
  18.             If (TextBox1.Text.Substring(iLoc).Length > 2) And Char.IsControl(e.KeyChar) = False Then
  19.                 e.Handled = True
  20.             End If
  21.         End If
  22.  
  23.     End Sub
  24.  
  25.  
Mar 7 '10 #2
Frinavale
9,735 Expert Mod 8TB
You could also consider using a MaskedTextBox. Specify that only 2 decimal places should be allowed.


-Frinny
Mar 12 '10 #3
!NoItAll
297 100+
Perhaps - but that control is creepy. I don't think users understand it, and getting it to behave the way you really want I've found to be more difficult than the simple routine above. The routine is small, fast and clean. You can assign it to multiple textboxes and bang, Bob's your uncle.
Mar 12 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
8
by: nick | last post by:
printf("%lf",3.25); the result is 3.25000 i want the answer correct to 3 decimal places What should i do? thanks!
1
by: nic | last post by:
Hi, I have a textbox value that I need to force (and format) to be a numeric value. For example, I want to allow a user to type 1000 or 1,000 or 1,000,000 but not something that is not numberic....
1
by: Kum | last post by:
Hi, I need help in asp.net dynamic textbox controls validation. I am creating textbox controls dynamically on a asp.net webpage. Now after creating the textboxes on the page I want to validate...
11
by: Keith | last post by:
I apologize for those of you who think I'm posting on the same topic. It is not that I don't appreciate all of your comments - and I'm definitely reading them all - but I think I have a differing...
1
by: Mike | last post by:
Hi All; I have created a MSDE SQL table with a column set to money, format is standard with 2 decimal places. Into the field I put 12345.67. When you view the data it shows 12,345.67. When I...
2
by: Agnes | last post by:
In my Datagrid, I will sum up the input value and store into decTtlFDebit E.g DecTtlFDebit is 15 the textbox only display 15 I want the display is 15.00 , How can I do that ?? I try the...
3
by: NH | last post by:
How can I check the value of a textbox has no more than 2 decimal places? I already check that the value is numeric, but how do I check that it is not 0.223 or .022 etc. I only want to allow up...
27
by: code_wrong | last post by:
Visual Basic (not dot net) what is the best way to check the User has entered an integer into an InputBox? isNumeric() checks for a numeric value .. but does not notify of numbers with decimal...
4
by: ApexData | last post by:
I have a field set to 1 decimal place in the table definition. I have a textbox bound to the field also set to 1 decimal place. Why can I enter the number 1.57 in the textbox, and this becomes...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.