e.Handled =
True End If If NChars.IndexOf(e.KeyChar) = -1 Then ' Invalid Charactere.Handled =
True ElseIf e.Handled = False And tb.Text.Length = 0 Thentb.Text =
"$" + e.KeyChare.Handled =
Truetb.SelectionStart = 2
End If End SubPrivate
Sub NumericLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox12.Leave, TextBox11.Leave, TextBox10.Leave, TextBox24.Leave, TextBox22.Leave, TextBox21.Leave, TextBox20.Leave, TextBox19.Leave, TextBox18.Leave, TextBox17.Leave, TextBox16.Leave, TextBox15.Leave, TextBox14.Leave, TextBox13.Leave, TextBox4.Leave, TextBox26.Leave, TextBox25.Leave, TextBox23.Leave, TextBox27.Leave, TextBox31.Leave, TextBox30.Leave, TextBox29.Leave, TextBox28.Leave, TextBox7.Leave, TextBox32.Leave Dim tb As TextBox = sender If tb.Text.Length = 0 Then tb.Text = "$0.00" End SubPlease forgive the new guy for bad coding.
I am doing an Investment type Calc. 2 things. On pg tab 1 I am not able to use the backspace button while debugging do undo anything I input. I have to highlight to replace the numbers. Even by doing that I loose the"$". If I tab to the next box the"$" appears as it should but if I change the values in the text box "$" doesn't show anymore. Second questions is how can I modify the code so that it automaticly places the decimal point and comma( comma to seperate hundreds and thousands) so that if I input 123456 it appears in the text box as $123,456.00. I would like to not have to use the decimal key or comma key to do this. 3, How can I have the numericboxes show "$0.00" on page load. Because as of now I have to tab through them the get "$0.00" if I do not then one I do the Button click and if a text box is empty I get an error.
Thanks for the help.

Textbox confussion
Matheesha
The problem with a mask is that you have to specify how many characters you want. If you want to use a regular TextBox then I'd suggest a combination of the Enter event to remove formatting, the Validating event to validate the value and the Validated event to add formatting. I strongly suggest NOT using the Leave event as it is not appropriate in this case:
Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter Dim myTextBox As TextBox = CType(sender, TextBox) Dim result As Decimal 'Remove currency formatting. If Decimal.TryParse(myTextBox.Text, Globalization.NumberStyles.Currency, Nothing, result) ThenmyTextBox.Text = result.ToString()
End If End Sub Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating Dim myTextBox As TextBox = CType(sender, TextBox) 'Make sure the user has eneterd a valid amount, allowing the currency symbol or not. If myTextBox.Text.Trim() <> String.Empty AndAlso _ Not Decimal.TryParse(myTextBox.Text, Globalization.NumberStyles.Currency, Nothing, New Decimal) Then 'Don't let the user leave the current control with an invalid value.myTextBox.HideSelection =
FalsemyTextBox.SelectAll()
MessageBox.Show(
"Please enter a valid value.")myTextBox.HideSelection =
Truee.Cancel =
True End If End Sub Private Sub TextBox1_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Validated Dim myTextBox As TextBox = CType(sender, TextBox) Dim result As Decimal If myTextBox.Text.Trim() = String.Empty Then 'Assume 0 for a empty string.result = 0D
Else 'Get the entered value, allowing the currency symbol or not.result =
Decimal.Parse(myTextBox.Text, Globalization.NumberStyles.Currency) End If 'Format the value as currency.myTextBox.Text = result.ToString("c"c)
End SubNote that this code allows the user to enter the currency symbol themselves or omit it.
Edit: Note that you can also add additional validation to prevent the user entering illegal values in the first place, although it is not easy to do properly. You can handle the KeyPress event to reject illegal characters but you need to consider where the currency symbol can go and the fact that you are allowed only one. The same goes for the decimal point, and you have to worry about pasting illegal values from the clipboard. There are third-party solutions available that do this for you. The best I've seen is included in the free Windows Forms Components library from Quantum Software Solutions. Unfortunately there is not a native .NET 2.0 version available yet, although I'm assured that it is on the way.
xinz
hi,
you can try this
Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter Dim mytext As String = TextBox1.Text For Each chr As Char In mytext If Not Char.IsDigit(chr) Then 'remove anyformat from the string including the .mytext = Replace(mytext, chr,
"") 'add the . againmytext = mytext.Insert(mytext.Length - 2,
".") End If NextTextBox1.Text = mytext
End Sub
Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave Dim Mytext As Double = Double.Parse(TextBox1.Text)TextBox1.Text = Format(Mytext,
"C2") End Subhope this will help
Talimar
Have you tried the MaskedTextBox If you set the Mask to something like "$0,000.00", you will only be able to enter numbers, and it will be formatted accordingly.