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 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.
:)
8 7934
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.
:)
-
-
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(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
if the return value of Check_Decimal>0 then keys.handle=fal se 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, eg:
123.45
|
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...
|
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...
|
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...
| |
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
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!
|
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?
-----------------------------------------------------------------------
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.
|
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) & ",'"...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |