How to apply 2 mascara to the same textbox windows form c#

Asked

Viewed 590 times

3

I’m with 2 checkbox natural person and legal person, and only one textbox.

I want to select on checkbox individual, I want to apply the mask to CPF, or when selecting the legal person he applies the mask to CNPJ on textbox.

I don’t know apply the mask according to what I select.

  • 1

    At the click of the checkbox you check which is and changes the mask of the textbox,: Textbox1.Mask = "máscara";

  • use RadioButton for that reason

  • Sorry, I didn’t realize it was Windows Forms.

1 answer

5


Semantically speaking, it would be correct to use radio button in place of checkboxes, since the user can only mark one of the options.

Anyway, the implementation is exactly the same:

In the event of checked of the element defining the type of person (the checkbox) you must do something like this

private void rbPessoaFisica_CheckedChanged(object sender, EventArgs e)
{
    txtDocumento.Mask = rbPessoaFisica.Checked ? @"000\.000\.000\-00" : @"00\.000\.000\/0000\-00";
}

See working below

Máscara dinâmica funcionando

  • Thanks, solved my problem, I was using a TextBox, instead of using a MaskedTextBox, when I was doing txtCpfCnpj.Mask showed a reference error, so I switched from TextBox for MaskedTextBox, then I can apply the mask according to what I select in the checkbox. I appreciate the help!!!

  • LINQ, the one you used to capture an animated gif?

  • 2

    @Ricardopunctual Screentogif

Browser other questions tagged

You are not signed in. Login or sign up in order to post.