Back on my form with a ton of financial data entry textboxes but I want to have the data formatted at runtime, as I enter the data.

I have a format declared as

Public Const cFmtCurrency = "##,###,###"    ' don't need decimal for the local China currency

What would be the least compicated way to have these textboxes (19 of them) format the Long Integer values as I enter the data. I think a Lost Focus event would be silly; there must be a better, more efficient way.

Also want to use this format when I retreive records from the db (don't see a reason to mask data in db)

Ideas, comments, suggestion?

Recommended Answers

All 7 Replies

set a masked Text box and set the mask as 00,000,000 and the database datatype can be varchar to retrieve this data....hope it helps u....

The datatype should not be varchar. Storing numbers as numbers allows you to to calculations, perform aggregate functions and to compare and sort results correctly. If you store numbers as strings you lose all that functionality. Plus, using strings will likely use more storage. It's easy enough to format the data for display after retrieval from the database.

You could do something along these lines:

Dim myFormat As Integer = CInt(PriceTextBox.Text)
PriceTextBox.Text = Format(myFormat, "##,###,###")

Hope this helps :)

I'll try both approaches here. I think (scarey) that mikeybware might be the simpler way without adding resources but I'll need to do that with a form wide scope; which shouldm't be too difficult.

@poojavb,

I understand this 'masked textbox' for the most part but I do not want the literals displayed. I just want the values to display the 1000 separator.

Can you check this and advise?

See if this helps.

Public Class Form1
    Public Const cFmtCurrency As String = "##,###,###" ' don't need decimal for the local China currency

    Private Sub setCoolHandlersForTextBoxes()
        For Each myCoolTextBox In New TextBox() {TextBox1, TextBox2, TextBox3}
            AddHandler myCoolTextBox.Validated, AddressOf _myCoolTextBoxes_Validated '// set the event to be handled by each TextBox.
        Next
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        setCoolHandlersForTextBoxes()
    End Sub

    Private Sub _myCoolTextBoxes_Validated(ByVal sender As Object, ByVal e As System.EventArgs)
        With CType(sender, TextBox)
            If Not .Text = "" AndAlso IsNumeric(.Text) Then .Text = CInt(.Text).ToString(cFmtCurrency) '// Format.
        End With
    End Sub
End Class

This link might also help.

@ codeorder

Yah, this one does the trick after I figured it out and made some minor changes;.....changes for my particular project. Good on ya'

I'm curious whether there is a way to 'cast a net' over all the textboxes at once instead of having to enumerate in this line(5)...

For Each myCoolTextBox In New TextBox() {TextBox1, TextBox2, TextBox3, etc., etc., etc}

would there be a way to say it like:

For Each myCoolTextBox In New TextBox() {myTextboxes} ' where mytextboxes is collection of all textboxes on my form?

Or perhaps is there a way to have a LostFocus event based on the form meaning everytime a textbox loses focus on my form?

LIke to hear your thought, ideas, suggestions

In any case, thanks codeorder

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.