Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WF
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 7-Jun-11 1:09am
Edited 7-Jun-11 1:15am
v2
Comments
SAKryukov at 7-Jun-11 13:18pm
   
What does this tag mean: "WF"?!!
--SA
Keith Barrow at 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.
SAKryukov at 7-Jun-11 17:03pm
   
LOL! It's great. This exactly describes my reaction to this question!
--SA
Keith Barrow at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
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
  Permalink  
v3

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

  Print Answers RSS
0 Nirav Prabtani 268
1 OriginalGriff 217
2 Mika Wendelius 185
3 CPallini 170
4 _Amy 170
0 Nirav Prabtani 284
1 OriginalGriff 217
2 Mika Wendelius 185
3 _Amy 170
4 Sergey Alexandrovich Kryukov 154


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 8 Jun 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100