Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i made code to format number when total lengh is == 11, it run on texbox change, but only format when it have 11 characters, i would like to make it on runtime (live), understood ? Its possible ? See my code:

private void textBox3_TextChanged(object sender, EventArgs e)
        {

            Int64 cpf = Convert.ToInt64(textBox3.Text);
            if (textBox3.TextLength == 11)
            {
                textBox3.Text = string.Format(@"{0:000\.000\.000-00}", Convert.ToInt64(cpf));
            }
        }

Thanks

share|improve this question
1  
Nope, I didn't understand what means make it on runtime – lazyberezovsky Apr 13 at 11:19
1  
@lazyberezovsky, I was literally thinking the same thing! It's in runtime already. :D – Michael Perrenoud Apr 13 at 11:19
i am braziliam, not speak english very well.. What i mean is, for example, if user type 1, it will convert to 000.000.000-01, if type 12, it will convert to 000.000.000-12, if type 123, it will convert to 000.000.001-23, undertood ? intead wait user type all numbers (ex: 11804852598 to convert to 118.048.525-98) it will make live (problem that i found that when make live it change textbox value), understood now ? – felipe Salomao Apr 13 at 11:24

2 Answers

up vote 0 down vote accepted

As lazyberezovsky stated, use a masked textbox, but set the PromptChar to whatever you want. Something along the lines of:

//In your form_load
//Based on your code above, assuming textBox3 is a MaskedTextbox    
textBox3.KeyUp += CheckEvent()
textBox3.Mask = "000000000000";
textBox3.PromptChar = 'x'; //set this to a space or whatever you want ' ' for blank!

//check AFTER every key press
private void CheckEvent(object Sender, KeyEventArgs e)
{
    if(textBox3.Text.Count() < 12)
    {
        return;
    }

    //change the textboxMask when all chars present
    maskedTextBox1.Mask = "0:000.000.000-00";
}
share|improve this answer
its near perfect, but for example, when i add this: on mask 000.000.000-00 and promptchar as " ", my field before user type show: " . . - ", i would like to hide these characters, only show them after user type values in field, like: if type 118, show 118., if type 118048, show 118.048, understood ? Hide . and - until user type qty to arrive that character.. Thanks – felipe Salomao Apr 13 at 12:26
Take a look at the edited code, this isn't perfect, but have a play around with it, the concept is to check when a key has been pressed using the KeyUp event and if all the numbers are present (12), change the mask. – Hassan Ghoncheh Apr 13 at 13:24
I edited it, its returning error: Error 1 No overload for method 'CheckEvent' takes 0 arguments – felipe Salomao Apr 13 at 14:30
worked, code is good, thanks for that. It hide all characters like . and - until i select field and start type, on first letter start formating, but not make it dinamical, like pure maskedtextbox does, had to have option to hide . and - in runtime.. – felipe Salomao Apr 13 at 14:37

Consider to use MaskedTextbox with Mask equal to 000.000.000-00. It will fill mask in usual way from left to right. Input will look like:

___.___.___-__

When use types 1 it will show 1__.___.___-__. When use types 12 it will show 12_.___.___-__. And so on.

share|improve this answer
I know this command, problem are these ___ that i dont want.. know how change it for " " instead "_" ? – felipe Salomao Apr 13 at 11:52
@felipeSalomao underscores show user positions that should be filled. As I understand you need all numbers to be typed. So, that will show to user that input is not finished yet. – lazyberezovsky Apr 13 at 11:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.