473,320 Members | 1,828 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,320 software developers and data experts.

One decimal point, and 2 numbers :)

5
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 unitPriceTextBox 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 7926
hariharanmca
1,977 1GB
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 unitPriceKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles unitPriceTextBox.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", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End Sub

everything works fine! BUT, i only want my unitPriceTextBox 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 1GB
Would you kindly post an explanation for this code snippet and some comments? Thanks
Feb 24 '07 #3
hariharanmca
1,977 1GB
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 1GB
Excellent, thanks. It is the policy of TSDN not to post code without explanations.
Feb 24 '07 #5
iDaz
5
THANK YOU VERY MUCH FOR YOUR HELP!!!!!

i really do appreicate it
Feb 25 '07 #6
iDaz
5
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 1GB
hariharanmca i have an error with your code

Public Function Check_Decimal(pKeyascii 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(Trim(pStrValue), InStr(1, pStrValue & Chr(pKeyascii), "."), Len(Trim(pStrValue) - 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(pStrValue & 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 1GB
if the return value of Check_Decimal>0 then keys.handle=false 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
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
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,...
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....
7
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...
687
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...
10
by: Paul Sullivan | last post by:
decimal d; d = 1.1M OR d= (decimal) 1.1 Discussioon
11
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...
13
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
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
1
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.