This thing has particularly interested me because I never thought in any way that this is possible (due to the fact that I don't know how to create one). While I was looking for tutorials/articles that can help me solve my problem about formatting a textbox to display currency during/after input, I found this article, and to sum it up if you don't want to open the link, it has a downloadable file that contains a pre-formatted currency textbox (I haven't used this though, because I don't know how to install/use it). But now that I have solved my own currency textbox issue (I mean, I have coded my own currency textbox), I thought that maybe I could make my own currency textbox that can be used on any project (I have to format 50+ currency textboxes and I think copy-pasting my code for all of those textboxes will be very messy and confusing).

Can anyone of you teach me how to do this or do you have any idea on how to do this? Thank you very much everyone. :)

BTW, here is my own code if you need to see it:

    Private Sub txtAmount1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs)
        Dim LenStr As String = Len(TextBox1.Text)
        Dim RawString As String
        LenStr = LenStr - 4
        If InStr(1, txtAmount1.Text, "PHP") <> 0 Then
            RawString = Microsoft.VisualBasic.Right(txtAmount1.Text, LenStr)
            RawString = RawString.Replace(",", "")
        Else
            If Double.TryParse(txtAmount1.Text, vbNull) Then
                txtAmount1.Text = FormatNumber(txtAmount1.Text, 2, TriState.False, , TriState.True)
                'TextBox1.Text = "PHP " + TextBox1.Text
                txtAmount1.BackColor = Color.White
            Else
                txtAmount1.BackColor = Color.DarkRed
                e.Cancel = True
            End If
        End If
    End Sub

It looks to me that you have 2 main options.

1 - In the load event handler loop through the Textboxes and add your code as a validating event handler, or whichever one seems appropriate.

2 - Change all the textboxes to a custom control that inherits TextBox and validates the text automatically, using your code. This is relatively simple, since you're only adding your code as an event handler. You can do this in the New constructor of the class.

For either of these option you need to change your code to use sender cast as the control.

For instance if your custom control is named CurrencyTextBox
Add this line:
Dim CurrentCTB = DirectCast(sender, CurrencyTextBox)
Then change all references to 'txtAmount1', to 'CurrentCTB'.

Edited 2 Years Ago by tinstaafl

This article has been dead for over six months. Start a new discussion instead.