Formatting Bound Textboxes [RESOLVED]
How can I format a bound text box? I've got something like the following:
Code:
txtCRate1.DataBindings.Add(New Binding("Text", dataset1, "mytable.Amt1"))
The value being bound to the text box would be something like 0.0645. It needs to be formatted like $0.064500. Any help would be appreciated.
The answer was:
Code:
Dim crate1 As Binding = New Binding("Text",ds, "mytable.Amt1")
AddHandler crate1.Format, AddressOf DecimalToCurrencyString
AddHandler crate1.Parse, AddressOf CurrencyStringToDecimal
txtCRate1.DataBindings.Add(crate1)
Private Sub DecimalToCurrencyString(ByVal sender As Object, ByVal cevent As _
ConvertEventArgs)
' The method converts only to string type. Test this using the DesiredType.
If Not cevent.DesiredType Is GetType(String) Then
Exit Sub
End If
' Use the ToString method to format the value as currency ("c").
If IsDBNull(cevent.Value) = False Then
cevent.Value = CType(cevent.Value, Decimal).ToString("c6")
Else
cevent.Value = CType(0, Decimal).ToString("c6")
End If
End Sub
Private Sub CurrencyStringToDecimal(ByVal sender As Object, ByVal cevent As _
ConvertEventArgs)
' The method converts back to decimal type only.
If Not cevent.DesiredType Is GetType(Decimal) Then
Exit Sub
End If
' Converts the string back to decimal using the static ToDecimal method.
cevent.Value = Decimal.Parse(cevent.Value.ToString, Globalization.NumberStyles.Currency, Nothing)
End Sub