473,651 Members | 3,007 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

One decimal point, and 2 numbers :)

5 New Member
hi everyone!
i've searched many sites including this one for the answer to me question, but i'm still in a bit of trouble. this is my current code...
Expand|Select|Wrap|Line Numbers
  1.  Private Sub unitPriceKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles unitPriceTextBox.KeyPress
  2.         If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar) Or e.KeyChar = ".") Then
  3.             e.Handled = True
  4.             MessageBox.Show("A price can only include numeric digits.", "Numeric Characters Only", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  5.         End If
  6.     End Sub
everything works fine! BUT, i only want my unitPriceTextBo x to accept ONE decimal point, and only allow TWO numbers maximum after the decimal point, eg. 1234.56 or 45.9 or 45.90. at the moment it obviously accepts unlimited decimal points and allow unlimited numbers after each decimal point.

if anyone could add to my code, or give me new code to help me out, i would greatly appreciate it!

thank you in advance.
:)
Feb 24 '07 #1
8 7934
hariharanmca
1,977 Top Contributor
hi everyone!
i've searched many sites including this one for the answer to me question, but i'm still in a bit of trouble. this is my current code...
Private Sub unitPriceKeyPre ss(ByVal sender As System.Object, ByVal e As System.Windows. Forms.KeyPressE ventArgs) Handles unitPriceTextBo x.KeyPress
If Not (Char.IsDigit(e .KeyChar) Or Char.IsControl( e.KeyChar) Or e.KeyChar = ".") Then
e.Handled = True
MessageBox.Show ("A price can only include numeric digits.", "Numeric Characters Only", MessageBoxButto ns.OK, MessageBoxIcon. Exclamation)
End If
End Sub

everything works fine! BUT, i only want my unitPriceTextBo x to accept ONE decimal point, and only allow TWO numbers maximum after the decimal point, eg. 1234.56 or 45.9 or 45.90. at the moment it obviously accepts unlimited decimal points and allow unlimited numbers after each decimal point.

if anyone could add to my code, or give me new code to help me out, i would greatly appreciate it!

thank you in advance.
:)

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub txtValue_KeyPress(KeyAscii As Integer)
  3.     KeyAscii = Check_Decimal(KeyAscii, Trim(txtValue.Text), 4, 2)
  4. End Sub
  5.  
  6. Public Function Check_Decimal(pKeyascii As Integer, pStrValue As String, Optional pOptNoDigit As Integer, Optional pOptNoDecimal As Integer) As Integer
  7.     If pKeyascii = vbKeyBack Or pKeyascii = vbKeyReturn Then Check_Decimal = pKeyascii: Exit Function
  8.     If Chr(pKeyascii) = "." And InStr(1, pStrValue, ".") > 0 Then Check_Decimal = 0: Exit Function
  9.     If Not ((pKeyascii >= 48 And pKeyascii <= 57) Or pKeyascii = 46) Then Check_Decimal = 0: Exit Function
  10.     If InStr(1, pStrValue & Chr(pKeyascii), ".") > 0 Then
  11.         If pOptNoDecimal > 0 Then
  12.             Check_Decimal = IIf(Len(Mid(Trim(pStrValue), InStr(1, pStrValue & Chr(pKeyascii), "."), Len(Trim(pStrValue) - InStr(1, pStrValue & Chr(pKeyascii), ".")))) > pOptNoDecimal, 0, pKeyascii)
  13.         Else
  14.             Check_Decimal = pKeyascii
  15.         End If
  16.     Else
  17.         If pOptNoDigit <= 0 Then pOptNoDigit = Len(pStrValue & Chr(pKeyascii))
  18.         Check_Decimal = IIf(Len(pStrValue & Chr(pKeyascii)) <= pOptNoDigit, pKeyascii, 0)
  19.     End If
  20. End Function

Use this code in Key press Event
Feb 24 '07 #2
willakawill
1,646 Top Contributor
Would you kindly post an explanation for this code snippet and some comments? Thanks
Feb 24 '07 #3
hariharanmca
1,977 Top Contributor
Would you kindly post an explanation for this code snippet and some comments? Thanks
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtValue_KeyPress(KeyAscii As Integer)
  2.     KeyAscii = Check_Decimal(KeyAscii, Trim(txtValue.Text), 4, 2)
  3. End Sub
  4.  
  5. Public Function Check_Decimal(pKeyascii As Integer, pStrValue As String, Optional pOptNoDigit As Integer, Optional pOptNoDecimal As Integer) As Integer
  6.     If pKeyascii = vbKeyBack Or pKeyascii = vbKeyReturn Then Check_Decimal = pKeyascii: Exit Function  ' Line 1
  7.     'Line 1 - Check for the Key value is Enter or Backspace then Retutn KeyAscii Value
  8.     If Chr(pKeyascii) = "." And InStr(1, pStrValue, ".") > 0 Then Check_Decimal = 0: Exit Function ' Line 2
  9.     'Line 2 - Check for the Key value is '. (Dot)' and already is there Dot then Retutn KeyAscii Value = 0
  10.     If Not ((pKeyascii >= 48 And pKeyascii <= 57) Or pKeyascii = 46) Then Check_Decimal = 0: Exit Function ' Line 3
  11.     'Line 3 - Check for the Key value is Numeric Only
  12.     'Check for Decimal Acailable
  13.     If InStr(1, pStrValue & Chr(pKeyascii), ".") > 0 Then
  14.     'True Part
  15.         'Check for Decimal Number value
  16.         If pOptNoDecimal > 0 Then
  17.             'True Part
  18.             'Get teh string Length after Decimal If Greater than pOptNoDecimal Return Key value 0 else Return Return Key
  19.             Check_Decimal = IIf(Len(Mid(Trim(pStrValue), InStr(1, pStrValue & Chr(pKeyascii), "."), Len(Trim(pStrValue) - InStr(1, pStrValue & Chr(pKeyascii), ".")))) > pOptNoDecimal, 0, pKeyascii)
  20.         Else
  21.             'False Part Return Key Value
  22.             Check_Decimal = pKeyascii
  23.         End If
  24.     Else
  25.     'False Part
  26.         'Check Number of Digit
  27.         If pOptNoDigit <= 0 Then pOptNoDigit = Len(pStrValue & Chr(pKeyascii))
  28.         Check_Decimal = IIf(Len(pStrValue & Chr(pKeyascii)) <= pOptNoDigit, pKeyascii, 0)
  29.     End If
  30. End Function
Ok
Feb 24 '07 #4
willakawill
1,646 Top Contributor
Excellent, thanks. It is the policy of TSDN not to post code without explanations.
Feb 24 '07 #5
iDaz
5 New Member
THANK YOU VERY MUCH FOR YOUR HELP!!!!!

i really do appreicate it
Feb 25 '07 #6
iDaz
5 New Member
hariharanmca i have an error with your code

Expand|Select|Wrap|Line Numbers
  1. Public Function Check_Decimal(pKeyascii As Integer, pStrValue As String, Optional pOptNoDigit As Integer, Optional pOptNoDecimal As Integer) As Integer
  2.         If pKeyascii = pKeyascii = Keys.Back Or pKeyascii = Keys.Enter Then Check_Decimal = pKeyascii : Exit Function ' Line 1
  3.         'Line 1 - Check for the Key value is Enter or Backspace then Retutn KeyAscii Value
  4.         If Chr(pKeyascii) = "." And InStr(1, pStrValue, ".") > 0 Then Check_Decimal = 0 : Exit Function ' Line 2
  5.         'Line 2 - Check for the Key value is '. (Dot)' and already is there Dot then Retutn KeyAscii Value = 0
  6.         If Not ((pKeyascii >= 48 And pKeyascii <= 57) Or pKeyascii = 46) Then Check_Decimal = 0 : Exit Function ' Line 3
  7.         'Line 3 - Check for the Key value is Numeric Only
  8.         'Check for Decimal Acailable
  9.         If InStr(1, pStrValue & Chr(pKeyascii), ".") > 0 Then
  10.             'True Part
  11.             'Check for Decimal Number value
  12.             If pOptNoDecimal > 0 Then
  13.                 'True Part
  14.                 'Get the string Length after Decimal If Greater than pOptNoDecimal Return Key value 0 else Return Return Key
  15.                 Check_Decimal = IIf(Len(Mid(Trim(pStrValue), InStr(1, pStrValue & Chr(pKeyascii), "."), Len(Trim(pStrValue) - InStr(1, pStrValue & Chr(pKeyascii), ".")))) > pOptNoDecimal, 0, pKeyascii)
  16.             Else
  17.                 'False Part Return Key Value
  18.                 Check_Decimal = pKeyascii
  19.             End If
  20.         Else
  21.             'False Part
  22.             'Check Number of Digit
  23.             If pOptNoDigit <= 0 Then pOptNoDigit = Len(pStrValue & Chr(pKeyascii))
  24.             Check_Decimal = IIf(Len(pStrValue & Chr(pKeyascii)) <= pOptNoDigit, pKeyascii, 0)
  25.         End If
  26.     End Function
in the first line, the comma after Integer...

As Integer, Optional pOptNoDecimal

has a blue wiggled underline, and my error says "Optional parameters must specify a value"

can you please help me out?

thanks in advance!
Feb 25 '07 #7
hariharanmca
1,977 Top Contributor
hariharanmca i have an error with your code

Public Function Check_Decimal(p Keyascii As Integer, pStrValue As String, Optional pOptNoDigit As Integer, Optional pOptNoDecimal As Integer) As Integer
If pKeyascii = pKeyascii = Keys.Back Or pKeyascii = Keys.Enter Then Check_Decimal = pKeyascii : Exit Function ' Line 1
'Line 1 - Check for the Key value is Enter or Backspace then Retutn KeyAscii Value
If Chr(pKeyascii) = "." And InStr(1, pStrValue, ".") > 0 Then Check_Decimal = 0 : Exit Function ' Line 2
'Line 2 - Check for the Key value is '. (Dot)' and already is there Dot then Retutn KeyAscii Value = 0
If Not ((pKeyascii >= 48 And pKeyascii <= 57) Or pKeyascii = 46) Then Check_Decimal = 0 : Exit Function ' Line 3
'Line 3 - Check for the Key value is Numeric Only
'Check for Decimal Acailable
If InStr(1, pStrValue & Chr(pKeyascii), ".") > 0 Then
'True Part
'Check for Decimal Number value
If pOptNoDecimal > 0 Then
'True Part
'Get the string Length after Decimal If Greater than pOptNoDecimal Return Key value 0 else Return Return Key
Check_Decimal = IIf(Len(Mid(Tri m(pStrValue), InStr(1, pStrValue & Chr(pKeyascii), "."), Len(Trim(pStrVa lue) - InStr(1, pStrValue & Chr(pKeyascii), ".")))) > pOptNoDecimal, 0, pKeyascii)
Else
'False Part Return Key Value
Check_Decimal = pKeyascii
End If
Else
'False Part
'Check Number of Digit
If pOptNoDigit <= 0 Then pOptNoDigit = Len(pStrValue & Chr(pKeyascii))
Check_Decimal = IIf(Len(pStrVal ue & Chr(pKeyascii)) <= pOptNoDigit, pKeyascii, 0)
End If
End Function


in the first line, the comma after Integer...

As Integer, Optional pOptNoDecimal

has a blue wiggled underline, and my error says "Optional parameters must specify a value"

can you please help me out?

thanks in advance!

Of course, in .Net the Optional value need some default value at the time of declaration pOptNoDegit as integer = 0,pOptNoDecimal as Integer=0
Feb 25 '07 #8
hariharanmca
1,977 Top Contributor
if the return value of Check_Decimal>0 then keys.handle=fal se else true
is for .Net
Feb 25 '07 #9

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

Similar topics

21
4518
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
4
8971
by: Enno Middelberg | last post by:
Hi, I'm sure someone asked this question before, but I can't find the solution on the web or in the groups. I want to print out columns of numbers which are aligned at the decimal point, eg: 123.45
17
6135
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. "Decimal Number" sometimes serves to distinguish Base 10 numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal Number" serves to differentiate a number from an integer. For the rest of this post I shall only use either...
7
43160
by: hana1 | last post by:
Hello experts, I used to program in C/C++ and now switched to Java. I am having a difficulty that I need your help with. How can I limit a double variable to hold 2 decimal points only? Say I have an array of 50 doubles that each ahs a number such as 23.9918444. I want to round round this number to 23.99 and any other calculations done on it should have the same precision. I know that Decimal Format does the rounding but the thing that...
687
23317
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't believe that. in pieces of the application where speed really matters you can still use "normal" functions or even static methods which is basically the same. in C there arent the simplest things present like constants, each struct and enum...
10
8120
by: Paul Sullivan | last post by:
decimal d; d = 1.1M OR d= (decimal) 1.1 Discussioon
11
2238
by: Pieter | last post by:
Hi, I'm having some troubles with my numeric-types in my VB.NET 2005 application, together with a SQL Server 2000. - I first used Single in my application, and Decimal in my database. But a Single with value 4.475 was converted to a Decimal with value 4.4749999999999996D. So after inserting and selecting it from the database I got another value than the original!
13
42870
by: Girish Sahani | last post by:
Hi, I want to truncate every number to 2 digits after the decimal point. I tried the following but it doesnt work. >>> a = 2 >>> b = 3 >>> round(a*1.0 / b,2) 0.67000000000000004
28
5867
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? ----------------------------------------------------------------------- When formatting money for example, to format 6.57634 to 6.58, 6.5 to 6.50, and 6 to 6.00? Rounding of x.xx5 is uncertain, as such numbers are not represented exactly. See section 4.7 for Rounding issues.
1
3444
by: Twanne | last post by:
Hi, I've got some code in VBA where I calculate some numbers. Now some of them are large decimal numbers like -6,3216324E03. So in scientific notations. Now I need to add them to a table with an insert query. The query: db.Execute ("INSERT INTO tmpZscore (datum, leeftijd, lengte, ZscoreL, gewicht, ZscoreW, BMI, ZscoreB, IBW) VALUES('" & CStr(dag & "/" & maand & "/" & Jaar) & "','" & test & "','" & CStr(lengte) & "'," & CDbl(scoreL) & ",'"...
0
8278
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8807
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8701
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7299
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1912
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.