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.
:)
8 7926
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.
:)
-
-
Private Sub txtValue_KeyPress(KeyAscii As Integer)
-
KeyAscii = Check_Decimal(KeyAscii, Trim(txtValue.Text), 4, 2)
-
End Sub
-
-
Public Function Check_Decimal(pKeyascii As Integer, pStrValue As String, Optional pOptNoDigit As Integer, Optional pOptNoDecimal As Integer) As Integer
-
If pKeyascii = vbKeyBack Or pKeyascii = vbKeyReturn Then Check_Decimal = pKeyascii: Exit Function
-
If Chr(pKeyascii) = "." And InStr(1, pStrValue, ".") > 0 Then Check_Decimal = 0: Exit Function
-
If Not ((pKeyascii >= 48 And pKeyascii <= 57) Or pKeyascii = 46) Then Check_Decimal = 0: Exit Function
-
If InStr(1, pStrValue & Chr(pKeyascii), ".") > 0 Then
-
If pOptNoDecimal > 0 Then
-
Check_Decimal = IIf(Len(Mid(Trim(pStrValue), InStr(1, pStrValue & Chr(pKeyascii), "."), Len(Trim(pStrValue) - InStr(1, pStrValue & Chr(pKeyascii), ".")))) > pOptNoDecimal, 0, pKeyascii)
-
Else
-
Check_Decimal = pKeyascii
-
End If
-
Else
-
If pOptNoDigit <= 0 Then pOptNoDigit = Len(pStrValue & Chr(pKeyascii))
-
Check_Decimal = IIf(Len(pStrValue & Chr(pKeyascii)) <= pOptNoDigit, pKeyascii, 0)
-
End If
-
End Function
Use this code in Key press Event
Would you kindly post an explanation for this code snippet and some comments? Thanks
Would you kindly post an explanation for this code snippet and some comments? Thanks
-
Private Sub txtValue_KeyPress(KeyAscii As Integer)
-
KeyAscii = Check_Decimal(KeyAscii, Trim(txtValue.Text), 4, 2)
-
End Sub
-
-
Public Function Check_Decimal(pKeyascii As Integer, pStrValue As String, Optional pOptNoDigit As Integer, Optional pOptNoDecimal As Integer) As Integer
-
If pKeyascii = vbKeyBack Or pKeyascii = vbKeyReturn 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 teh 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
Ok
Excellent, thanks. It is the policy of TSDN not to post code without explanations.
THANK YOU VERY MUCH FOR YOUR HELP!!!!!
i really do appreicate it
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!
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
if the return value of Check_Decimal>0 then keys.handle=false else true
is for .Net
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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,...
|
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....
|
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...
|
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...
|
by: Paul Sullivan |
last post by:
decimal d;
d = 1.1M
OR
d= (decimal) 1.1
Discussioon
|
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...
|
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
|
by: FAQ server |
last post by:
-----------------------------------------------------------------------
FAQ Topic - How do I convert a Number into a String with
exactly 2 decimal places?...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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...
| | |