OK - Lets try this.
Place 2 text boxes on a form and also a button.
Add my code to the form.
Enter 123 in textbox1 and press tab or click on textbox2 to terminate input.
Notice that textbox1 has been reformatted to display currency $123.00
Now, enter 456 in textbox2 and click button to set textbox1.text=textbox2.text
Notice that textbox1 now shows 456 not $456.00 as I want.
Must somehow have an event that responds to the assignment that executes the following:
TextBox1.Text = Convert.ToDecimal(Decimal.Parse(TextBox1.Text, Globalization.NumberStyles.Currency)).ToString("c")
Code:
Public Class Form2
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim DecSep As String = Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator
If e.KeyChar = Chr(8) Then
Exit Sub
ElseIf Char.IsNumber(e.KeyChar) = False Then
If e.KeyChar = DecSep Then
Exit Sub
Else
e.Handled = True
Exit Sub
End If
End If
End Sub
Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
Dim currency As Decimal
'Convert the current value to currency, with or without a currency symbol.
If Not Decimal.TryParse(TextBox1.Text, _
Globalization.NumberStyles.Currency, _
Nothing, _
currency) Then
'Don't let the user leave the field if the value is invalid.
With TextBox1
.HideSelection = False
.SelectAll()
MessageBox.Show("Please enter a valid currency amount.", _
"Invalid Value", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error)
.HideSelection = True
End With
e.Cancel = True
End If
End Sub
Private Sub TextBox1_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Validated
'Display the value as local currency.
TextBox1.Text = Convert.ToDecimal(Decimal.Parse(TextBox1.Text, Globalization.NumberStyles.Currency)).ToString("c")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = TextBox2.Text
End Sub
End Class