Maskedtextbox how to put mask dynamically in Windows Forms C#

Asked

Viewed 51 times

0

How do I hide the mask and only show it as the user fills the field (dynamically)? E.g.: In a xx/xx/xxxx date the bar appears only when it arrived at it. In the events of the component I only found something related when the focus is changed to the component, when I click on the component or when I press some keyboard key, but nothing that does that way. I’m using Windows Forms C# . Netframework 4.8 Visual Studio 2019

  • The answer is in your question. To create this effect you will need to use all these events and implement their behaviors.

1 answer

0

To create dynamically Windows you can initially put one MaskedTextBox maskedTextBox = new MaskedTextBox(); and put all the settings you want and to instate, a Controls.Add(maskedTextBox);. And to do what you want, Voce can do, so as each string size adds the bar in a text change event, look at an example, I used the Method DynamicallyMaskedTexBox() to create the Maskedtextbox and maskedTextBox_TexChanged(object sender, System.EventArgs e) for the event, look at the example:

namespace Forms
{
    public partial class Form1 : Form
    {
        private MaskedTextBox maskedTextBox;

        public Form1()
        { 
            InitializeComponent();
            DynamicallyMaskedTexBox();
        }

        private void DynamicallyMaskedTexBox()
        {
            
            maskedTextBox = new MaskedTextBox();
            maskedTextBox.Name = "MaskedTexBox1";
            maskedTextBox.Location = new Point(12,70);
            maskedTextBox.Width = 100;
            maskedTextBox.Height = 20;
            
            maskedTextBox.TextChanged += new EventHandler(maskedTextBox_TexChanged);

            Controls.Add(maskedTextBox);
        }

        private void maskedTextBox_TexChanged(object sender, System.EventArgs e)
        {
            string maskedText = "00000000";
            try
            {
                if(maskedTextBox.Mask.Length < maskedText.Length/4)
                {
                    maskedTextBox.Mask += "00";
                    maskedTextBox.SelectionStart = maskedTextBox.Text.Length;
                    maskedTextBox.SelectionLength = 0;
                }
                else if(maskedTextBox.Mask.Length == maskedText.Length/4)
                {
                    maskedTextBox.Mask += "/";
                    
                }
                else if(maskedTextBox.Mask.Length < maskedText.Length/2+1)
                {
                    maskedTextBox.Mask += "0";
                }
                else if (maskedTextBox.Mask.Length == maskedText.Length / 2+1)
                {
                    maskedTextBox.Mask += "/";
                }
                else if(maskedTextBox.Mask.Length >= maskedText.Length /2+2 && maskedTextBox.Mask.Length < maskedText.Length+2)
                {
                    maskedTextBox.Mask += "0";
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
}

Browser other questions tagged

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