Stopping After 2 Decimal Places  
Author Message
Learning VB





PostPosted: Visual Basic Express Edition, Stopping After 2 Decimal Places Top

This is probably a very easy, and very noob question.

In one project we are required to except the input of numbers in the dollar amount form: xx.xx

I can easily do this and also make sure that user only inputs numbers a period and can use the back space within the text box.

The problem I am not able to get around is only allowing the user to input two numbers after the decimal point. Currently I am using the following:

lblChange1.Text = FormatNumber(Change.ToString, 2)

Is there an easy to understand, non-existing code destroying way to fix that issue I have tried using masked textbox and no better results.

Thanks



Visual Studio Express Editions3  
 
 
gudel





PostPosted: Visual Basic Express Edition, Stopping After 2 Decimal Places Top

I used maskedtextbox, and in mask property I put 00.00. It did not allow me to enter more than two numbers after the decimal point. What did you do with your maskedtextbox that didn't work


 
 
Learning VB





PostPosted: Visual Basic Express Edition, Stopping After 2 Decimal Places Top

To be honest I dont remember.....any suggestions


 
 
Tall Dude





PostPosted: Visual Basic Express Edition, Stopping After 2 Decimal Places Top

See my posts at

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=669951&SiteID=1

and

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=688095&SiteID=1&mode=1



 
 
Learning VB





PostPosted: Visual Basic Express Edition, Stopping After 2 Decimal Places Top

I assume you are meaning:

If txtPurchase1.Text.IndexOf(".") < txtPurchase1.Text.Length - 3 Then

Ignore = True

txtPurchase1.Text = Mid(txtPurchase1.Text, 1, txtPurchase1.Text.Length - 1)

Ignore = False

End If

As I understand your code you are calling Ignore as a Boolean, however, when I use this and make Public ignore As Boolean = False.....it doesnt work.


 
 
Learning VB





PostPosted: Visual Basic Express Edition, Stopping After 2 Decimal Places Top

Okay so I went back and looked and what doesnt work for the MaskedTextBox as I need it.

The issue is not stopping after two decimal places, the issue arises when you must enter 8.90 versus 89.00. The syntax : Me.mtxtPurchase.Mask = "##.##" causes the user to arrow back one to be able to use 8.90....unlike a normal textbox you must enter numbers from the left not the right. If you have only .10 to enter you must arrow over twice....this does not work for the project that I am working on.

Any suggestions

Thanks


 
 
nobugz





PostPosted: Visual Basic Express Edition, Stopping After 2 Decimal Places Top

Here's a completely different approach:

Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
If TextBox1.Text = "" Then Return
Try
TextBox1.Text = String.Format("{0:N2}", CDbl(TextBox1.Text))
Catch ex As Exception
TextBox1.Text = "Invalid!"
TextBox1.SelectionStart = 0
TextBox1.SelectionLength = TextBox1.Text.Length
e.Cancel = True
End Try
End Sub



 
 
Learning VB





PostPosted: Visual Basic Express Edition, Stopping After 2 Decimal Places Top

Doesnt that do the same thing as:

TextBox1.Text = FormatNumber (CDec(TextBox1.Text), 2)

It truncates the input after a btn click is done by the user.

What I am lloking for is a way to stop the user from inputing more than 2 digits after the decimal point.

I am looking done the road of Microsoft.VisualBasic.Right Function to do this.

Any suggestions


 
 
spotty





PostPosted: Visual Basic Express Edition, Stopping After 2 Decimal Places Top

Perhaps something simple like the following may help.


Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If CType(sender, TextBox).Text.Contains(".") Then
Dim s As String = sender.text
Dim i As Integer = InStr(s, ".") '//Find Position of decimal place
If i + 2 <= s.Length Then '//If try to write more than 2 decimal places then truncate it.
Me.TextBox1.Text = s.Substring(0, i + 2)
Me.TextBox1.SelectionStart = Me.TextBox1.Text.Length
End If
End If
End Sub

I think you'll find this will stop you typing more than two digits after the . - you may need to combine with keypress event to ensure only numbers, control characters and the decimal point are valid permitted keypresses. Well documented - simply do a search for numeric + textbox


 
 
Learning VB





PostPosted: Visual Basic Express Edition, Stopping After 2 Decimal Places Top

That works great, alot simplier than what I did....But what I finally did was this (snipped from the whole code):

Private Sub txtPay_KeyPress(ByVal sender As Object, _

ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPay.KeyPress

'Force User to Input Only Numbers, '.' And BackSpace

If IsNumeric(e.KeyChar) Or e.KeyChar = "." Then

If InStr(txtPay.Text, ".") > 0 Then

Dim Test_Text_Pay As String = Microsoft.VisualBasic.Right(txtPay.Text, _

(Len(txtPay.Text) - InStr(txtPay.Text, ".")))

If Len(Test_Text_Pay) = 2 Then

MessageBox.Show("Please use proper format. (i.e 20.00)", _

"Improper Format!", MessageBoxButtons.OK, MessageBoxIcon.Error)

e.Handled = True

End If

End If

ElseIf e.KeyChar <> ControlChars.Back And e.KeyChar <> "." Then

MessageBox.Show("Please use proper format. (i.e 20.00)", _

"Improper Format!", MessageBoxButtons.OK, MessageBoxIcon.Error)

e.Handled = True

End If

End Sub

So thanks to everyone for your help, I am still plugin away.....next up Loops and Sorting Data.....yea