Results 1 to 1 of 1

Thread: Formatting Bound Textboxes [RESOLVED]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2000
    Location
    Tennessee
    Posts
    279

    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
    Last edited by Dman; Apr 23rd, 2004 at 04:11 PM.
    A cynic knows the price of everything but the value of nothing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width