none
Formatting textbox for currency. Weird annoying bug ..

    Question

  • I am trying to format a textbox value to currency type.. I am succeeding but theres a slight problem..
     
    This is what I am doing..
     
    'This function sets the databinding for a textbox.
    Private Sub PrepareDatabindings()
     
            'Prepare Currency Databindings
     
            Dim bindOriginalPrice As Binding
            bindOriginalPrice = New Binding("text", objEditPackage, "OriginalPrice", True)
            txtOriginalPrice.DataBindings.Add(bindOriginalPrice)
            AddHandler bindOriginalPrice.Format, AddressOf FormatOriginalPrice
     
            dgPackageInformation.Columns("txtColumnExtended").DefaultCellStyle.Format = "c"
            dgPackageInformation.Columns("txtColumnPrice").DefaultCellStyle.Format = "c"
           
        End Sub
     
        'Method for Format event.
        Private Sub FormatOriginalPrice(ByVal sender As Object, _
                ByVal e As System.Windows.Forms.ConvertEventArgs)
     
            If e.DesiredType IsNot GetType(String) Then
                Exit Sub
            End If
     
            If e.Value Is DBNull.Value Then
     
            Else
                e.Value = Format(e.Value, "c")
            End If
     
        End Sub
     
    -------------------------------------------------------------
    Now at form load the text box value is not getting formatted.
    But when I change the value and the textbox loses focus, the value is correctly formatted
    to currency.
    Also at load this Format event is raised twice and e.value is also set to $xxx but somehow it is not getting displayed at formload.
    -------------------------------------------------------------
    I am opening this from on a button click of another form.
     
    So need some help on fixing this issue... thanks!!
    Friday, July 25, 2008 7:06 PM

Answers

  • You need to add the handler to the format event before you bind the textbox to the binding

     

            'Prepare Currency Databindings
     
            Dim bindOriginalPrice As Binding
            bindOriginalPrice = New Binding("text", objEditPackage, "OriginalPrice", True)
            AddHandler bindOriginalPrice.Format, AddressOf FormatOriginalPrice
            txtOriginalPrice.DataBindings.Add(bindOriginalPrice)

     

    Saturday, July 26, 2008 9:48 AM