Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old April 18th, 2007, 12:44 AM
Registered User
Join Date: Feb 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Restricting the # of digits after a decimal in a K

Greetings Everyone!
 I have a simple question in what is the correct way to restrict the number of digits after the decimal point? As shown with my code below I was able to create a textbox in which when a user enters value such as 1232.23232 is possible...however I want to make the textbox more user friendly I restricting digits after the decimal to 2. So it would look more like this 1232.23. Thank you for all your time..any guidance and help would be greatly appreciated.

Public Class Form1

Private Sub txtPayment_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPayment.KeyPress
Const x As Integer = 2
If e.KeyChar = Chr(8) Then Exit Sub

Select Case e.KeyChar
Case "0" To "9"
Case "."
If (InStr(txtPayment.Text, ".")) <> x Then
e.Handled = True
End If
Case Else
e.Handled = True
End Select

End Sub
End Class

Old April 18th, 2007, 07:52 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi

hi there...
idea (because there is many many ways to do this):

count the number of digits after the period and if you have two cancel the input...

by the way.. if you are using .Net, why are you using instr???



================================================== =========
Read this if you want to know how to get a correct reply for your question:
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
================================================== =========

Similar Threads
Thread Thread Starter Forum Replies Last Post
Mulitplication of two digits in Datagrid u_roc100@rediffmail.com Visual Studio 2005 0 June 10th, 2008 03:35 PM
Testing a string for digits bonekrusher XSLT 2 March 18th, 2007 02:48 PM
decimal data type all 0's after decimal gobotsoup SQL Server ASP 1 February 28th, 2007 02:32 PM
Number of Digits Colonel Angus Javascript How-To 3 December 1st, 2004 09:12 AM
Change number of significant digits displayed gurbani XSLT 1 June 25th, 2004 11:50 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.