Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I'm trying to set up mask in masked text box for decimal or real numbers only.
I've tried 999,999.00 or 000,000.00 or ###,###.##.
But when i'm trying input e.g. 23.45 value nothing changed after pressing , or . and
I receive 234 5__.__ value. I think after pressing , or . carret should move on decimal place.

How to do it? Has masked text box the possibility to set it up in that way?

Please help me in this.

Rafal
Posted
Updated 7-Jun-11 1:15am
v2
Comments
Sergey Alexandrovich Kryukov 7-Jun-11 13:18pm    
What does this tag mean: "WF"?!!
--SA
Keith Barrow 7-Jun-11 14:45pm    
It normally means Workflow Foundation, but I don't see WTF it has to do with this question :)
Sorry: couldn't resist.
Sergey Alexandrovich Kryukov 7-Jun-11 17:03pm    
LOL! It's great. This exactly describes my reaction to this question!
--SA
Keith Barrow 7-Jun-11 14:46pm    
Did you mean to tag this as WPF, we don't know what you are writing your UI in.

1 solution

sorry to say that the maskedtextbox control in a windows forms for either c# or VB.net is not as flexible as you would hope it to be, it currently allow a fixed width masked value only, as in you will have to type 00002345 to get 23.45 in the textbox. the masked value puts the comma and dot for you so you really don't have to press them.

you can achieve a similar behavior using a regular textbox control by doing the following:
1. Create a textbox control let's call it Textbox1
2. In the code behind add the following:
VB
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
    If TextBox1.Text = "___,___.__" Then
        'removes the mask once the mouse cursor is within the textbox
        TextBox1.Text = ""
    End If
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    Dim ListofChars As String = "0123456789.,"
    If ListofChars.IndexOf(e.KeyChar) = -1 Then
        e.Handled = True
    End If
End Sub
Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
    If TextBox1.Text = "" Then
        'puts the mask back if you didn't enter anything into the textbox once the mouse cursor leave the textbox
        TextBox1.Text = "___,___.__"
    End If
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    TextBox1.Text = TextBox1.Text.Trim
    If Not TextBox1.Text = "" And Not TextBox1.Text = "___,___.__" Then
        Dim aDecimal As Decimal = CType(TextBox1.Text, Decimal)
        TextBox1.Text = Format(aDecimal, "c").Replace("$", "")
    End If
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    TextBox1.Text = "___,___.__" 
End Sub
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900