1

I made my own Currency TextBox, here is code:

CurrencyTextBox.Designer.vb:

Partial Class CurrencyTextBox
    Inherits System.Windows.Forms.TextBox
    (...)
End Class

and CurrencyTextBox.vb:

Public Class CurrencyTextBox

Protected Overrides Sub OnKeyPress(e As Windows.Forms.KeyPressEventArgs)
    If Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar) Or
    e.KeyChar = Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator Then
        MyBase.OnKeyPress(e)
    Else
        e.Handled = True
    End If
End Sub

End Class

it works, but I don't know what to do with pasted text, there are a lot of examples here (Capture CTRL+V or paste in a textbox in .NET) how to catch CTRL+V, but no idea how to check if pasted text is "correct".

My idea is:

Dim text As String = Clipboard.GetText(TextDataFormat.Text)
For Each ch As Char In text
(...)
Next

but I thing it is not best way. Can I do it "better"?

Community
  • 1
  • 1
  • I provided an answer to your question, please tell me whether I have got the right idea as I didn't quite get your question. – AStopher Dec 07 '14 at 12:05
  • Hmm, Code in your answer should work of course, but I don't want to add handler. I'm using this "CurrencyTexBox" on few forms, few times per form, so it was be better to made own control inherit from TextBox and put whole code inside new "CurrencyTextBox" custom class. –  Dec 07 '14 at 14:28
  • Code in my question also work, but I m asking if is it better way to do it. Checking char by char in pasted text looks ugly(?) –  Dec 07 '14 at 14:32
  • Maybe better idea is to try convert pasted text into double and catch exception? What do you think? Something like line below closed in try...catch block `Double.Parse(Me.text, Globalization.NumberStyles.Currency)` –  Dec 07 '14 at 14:36
  • If that's the case, then your question isn't clear ***at all***. You have no choice but to add a subroutine for ` Handles TextBox1.TextChanged`, this is the *only* way the desired behaviour will occur. As my answer answered your question, please upvote it and mark it as accepted (click the tick next to my answer). – AStopher Dec 07 '14 at 18:49

1 Answers1

0

Should be:

Private Sub CurrencyTextBox_TextChanged(sender As Object, e As EventArgs) Handles CurrencyTextBox.TextChanged
    Dim text As String = Clipboard.GetText(TextDataFormat.Text)
    For Each ch As Char In text
        (...)
    Next

End Sub

The code I use in this situation is this:

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    If (Clipboard.GetText()) Then 'If we go straight to `Dim text...` then the application *could* crash, thus we filter out if they have any text on the clipboard first.
        Dim text As String = Clipboard.GetText()
        For Each ch As Char In text
            'Custom validation here.
        Next
    End If
End Sub

The key variable here is Handles CurrencyTextBox.TextChanged.

The above code executes whenever text is changed inside the textbox and runs whatever validation you require.

AStopher
  • 4,207
  • 11
  • 50
  • 75