Desenvolvedores.Net - TechBlog

MaskedNumber em C-Sharp

0
1 Estrela2 Estrelas3 Estrelas4 Estrelas5 Estrelas (1 votos, média: 5,00 de 5)
Loading...
13 de março de 2011
Quando eu precisei de um campo numérico em C#, tentei usar o MaskedTexBox.Eu particularmente não gostei deste componente e neste caso eu reinventei a roda e acabei criando o MaskedNumber.

Eu coloquei alguns tipos já pré-formatados como:
Peso, Valor, Moeda, Porcentagem, Telefone, CPF, CNPJ, CEP e Custom

Pode ser que seja útil pra muita gente, pode ser que não. Mas está aí. Resolvi disponibilizar ele aqui para compartilhar com o pessoal.

Ainda tem alguns errinhos que vou corrigindo aos poucos e se alguém alterar os fontes e quiser me mandar as versões. Eu agradeço.

[]’s

Código do componente

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Threading;

public enum MaskedNumberFormat
{
    Peso,
    Valor,
    Moeda,
    Porcentagem,
    Telefone,
    CPF,
    CNPJ,
    CEP,
    Custom
}

namespace MaskedNumber
{
    public partial class MaskedNumber : System.Windows.Forms.TextBox
    {
        #region Construtores
        public MaskedNumber()
        {
            InitializeComponent();
            this.TextAlign = HorizontalAlignment.Right;
            base.KeyPress += new KeyPressEventHandler(MaskedNumber_KeyPress);
            base.LostFocus += new EventHandler(MaskedNumber_LostFocus);
            this.Format = MaskedNumberFormat.Valor;
        }
        #endregion

        #region Eventos
        void MaskedNumber_LostFocus(object sender, EventArgs e)
        {
            this.Refresh();
        }

        void MaskedNumber_KeyPress(object sender, KeyPressEventArgs e)
        {

            switch (e.KeyChar.ToString())
            {
                case ",":
                case ".":
                    e.KeyChar = ',';
                    if (this.Text.Contains(e.KeyChar.ToString()) == true)
                    {
                        e.Handled = true;
                        e.KeyChar = '0';
                    }
                    break;
                case "+":
                    e.Handled = true;
                    e.KeyChar = '0';

                    if (Format == MaskedNumberFormat.Telefone)
                    {
                        if (!this.Text.Contains("+"))
                        {
                            this.Text = "+" + this.Text;
                            this.SelectionStart = this.Text.Length;
                        }
                    }
                    break;

                case "-":
                    e.Handled = true;
                    e.KeyChar = '0';

                    if (Format == MaskedNumberFormat.Moeda ||
                        Format == MaskedNumberFormat.Valor ||
                        Format == MaskedNumberFormat.Porcentagem ||
                        Format == MaskedNumberFormat.Peso)
                    {
                        if (!this.Text.Contains("-"))
                        {
                            this.Text = "-" + this.Text;
                            this.SelectionStart = this.Text.Length;
                        }
                    }
                    break;
                default:
                    if (!char.IsNumber(e.KeyChar) &&    //numero
                        e.KeyChar != '\b')            //backspace
                    {
                        e.Handled = true;
                        e.KeyChar = '0';
                    }
                    break;
            }
        }

        private void MaskedNumber_Enter(object sender, EventArgs e)
        {
            this.SelectAll();
        }

        private void MaskedNumber_Click(object sender, EventArgs e)
        {
            this.SelectAll();
        }
        #endregion

        #region Propriedades
        private MaskedNumberFormat format;
        private String mCustomFormat = "0:0";
        public String CustomFormat
        {
            get
            {
                if (mCustomFormat == null ||
                    mCustomFormat.Length == 0) mCustomFormat = "0:0";
                return mCustomFormat;
            }
            set
            {
                mCustomFormat = value;
                this.Refresh();
            }
        }

        public MaskedNumberFormat Format
        {
            get { return format; }
            set
            {
                format = value;
                this.Refresh();
            }
        }

        public double Value
        {
            get
            {
                double ret = 0;
                double.TryParse(global::MaskedNumber.Format.OnlyNumbers(base.Text).ToString(), out ret);

                //se começar com ( é numero negativo
                if (Format == MaskedNumberFormat.Moeda && base.Text.StartsWith("("))
                    ret *= -1;
                return ret;
            }
        }

        #endregion

        #region Overrides and New
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
                this.Refresh();
            }
        }

        public override void Refresh()
        {
            switch (Format)
            {
                case MaskedNumberFormat.Peso:
                    base.Text = String.Format("{0:n3}", this.Value);
                    break;
                case MaskedNumberFormat.Valor:
                    base.Text = String.Format("{0:n2}", this.Value);
                    break;
                case MaskedNumberFormat.Moeda:
                    base.Text = String.Format("{0:C}", this.Value);
                    break;
                case MaskedNumberFormat.Porcentagem:
                    base.Text = String.Format("{0:P2}", this.Value / 100);
                    break;
                case MaskedNumberFormat.CPF:
                    base.Text = global::MaskedNumber.Format.CPF(Text);
                    break;
                case MaskedNumberFormat.CNPJ:
                    base.Text = global::MaskedNumber.Format.CNPJ(Text);
                    break;
                case MaskedNumberFormat.CEP:
                    base.Text = global::MaskedNumber.Format.CEP(Text);
                    break;
                case MaskedNumberFormat.Telefone:
                    base.Text = global::MaskedNumber.Format.Telefone(Text);
                    break;
                case MaskedNumberFormat.Custom:
                    base.Text = global::MaskedNumber.Format.Custom(Text, CustomFormat);
                    break;
            }

            base.Refresh();
        }
        #endregion

        #region MaskedNumber.Designer.cs
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            //
            // MaskedNumber
            //
            this.Size = new System.Drawing.Size(150, 20);
            this.Enter += new System.EventHandler(this.MaskedNumber_Enter);
            this.Click += new System.EventHandler(this.MaskedNumber_Click);
            this.ResumeLayout(false);

        }

        #endregion
        #endregion
    }

    /// <summary>
    /// Classe responsável pelas formatações do componente.
    /// </summary>
    internal static class Format
    {
        /// <summary>
        /// Formata o CNPJ
        /// </summary>
        /// <param name="_cnpj">cnpj a ser formatado</param>
        /// <returns>cnpj formatado</returns>
        public static string CNPJ(string _cnpj)
        {
            MaskedTextProvider mascara;
            //cnpj
            //##.###.###/####-##
            mascara = new MaskedTextProvider(@"00\.000\.000/0000-00");
            mascara.Set(_cnpj);
            return mascara.ToString();
        }

        /// <summary>
        /// Formata o CPF
        /// </summary>
        /// <param name="_cpf">CPF a ser formatado</param>
        /// <returns>cpf formatado</returns>
        public static string CPF(string _cpf)
        {
            MaskedTextProvider mascara;
            //cpf
            //###.###.###-##
            mascara = new MaskedTextProvider(@"000\.000\.000-00");
            mascara.Set(_cpf);
            return mascara.ToString();
        }

        /// <summary>
        /// formata o cep e retorna
        /// </summary>
        /// <param name="_cep">cep a ser formatado</param>
        /// <returns>cep formatado como 00000-000</returns>
        public static string CEP(string _cep)
        {
            _cep = OnlyNumbers(_cep).ToString().Replace("-", "");
            _cep = _cep.ToString(null).PadRight(8, '0');
            return _cep.Substring(0, 5) + "-" + _cep.Substring(5, 3);
        }

        /// <summary>
        /// Formata um número de telefone e retorna o numero formatado
        /// </summary>
        /// <param name="value">número de telefone</param>
        /// <returns>telefone formatado</returns>
        public static string Telefone(string value)
        {
            if (String.IsNullOrEmpty(value)) return "";
            string mascara = "";
            value = value.Trim();
            if (value.StartsWith("0800") ||
                value.StartsWith("0300") ||
                value.StartsWith("0500") ||
                value.StartsWith("0900"))
                mascara = @"0000\-000\-0000";
            else if (value.StartsWith("+"))
                mascara = @"+00\(00\)0000\-0000";
            else if (value.Length == 7)
                mascara = @"000\-0000";
            else if (value.Length == 8)
                mascara = @"0000\-0000";
            else if (value.Length == 10)
                mascara = @"\(00\)0000\-0000";
            else if (value.Length == 11)
                mascara = @"\(000\)0000\-0000";
            else if (value.Length == 12)
                mascara = @"00\(00\)0000\-0000";
            else if (value.Length == 13)
                mascara = @"000\(00\)0000\-0000";
            else
                mascara = "0".PadLeft(value.Length, '0');

            MaskedTextProvider maskProvider;

            maskProvider = new MaskedTextProvider(mascara);
            maskProvider.Set(value);

            if (!maskProvider.MaskCompleted)
                return value;

            return maskProvider.ToString();
        }

        public static string Custom(string Text, string CustomFormat)
        {
            MaskedTextProvider mascara;
            mascara = new MaskedTextProvider(CustomFormat);
            mascara.Set(Text);
            return mascara.ToString();
        }

        /// <summary>
        /// extrair todo caracter não numérico do text, deixando apenas a virgula e os números
        /// <para>Permite número negativo</para>
        /// </summary>
        /// <param name="text">texto a ser tratado</param>
        /// <returns></returns>
        public static object OnlyNumbers(object text)
        {
            bool flagNeg = false;

            if (text == null || text.ToString().Length == 0) return 0;
            string ret = "";

            foreach (char c in text.ToString().ToCharArray())
            {
                if (c.Equals('.') == true || c.Equals(',') == true || char.IsNumber(c) == true)
                    ret += c.ToString();
                else if (c.Equals('-') == true)
                    flagNeg = true;
            }

            if (flagNeg == true) ret = "-" + ret;

            return ret;
        }
    }
}

Código do exemplo. Não esqueça de fazer referência ao componente se estiver criando ele em separado.
E não esquecer de colocar na mesma solução para depurar.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Teste
{
    //iniciar o sistema
    class StartUp
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmMain());
        }
    }

    public class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            linkLabel1.Text = "Se vc alterar os fontes e achar que isto foi útil. Não esqueça de enviá-los para mim. :)" + '\r' + '\n';
            linkLabel1.Text += "Visite meu site : http://desenvolvedores.net" + '\r' + '\n';
            linkLabel1.Text += "E-mail : projetos@desenvolvedores.net";

            linkLabel1.Links.Add(108, 26, "http://desenvolvedores.net");
            linkLabel1.Links.Add(145, 51, "mailto:projetos@desenvolvedores.net?subject=MaskedNumber");

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
        }

        private void maskedNumber4_TextChanged(object sender, EventArgs e)
        {

        }

        private void maskedNumber4_Enter(object sender, EventArgs e)
        {
            maskedNumber4.Format = MaskedNumberFormat.Custom;
            maskedNumber4.CustomFormat = txtFormatoCustom.Text;
        }

        #region frmMain.Designer
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.linkLabel1 = new System.Windows.Forms.LinkLabel();
            this.label6 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this.txtFormatoCustom = new System.Windows.Forms.TextBox();
            this.label8 = new System.Windows.Forms.Label();
            this.label9 = new System.Windows.Forms.Label();
            this.label10 = new System.Windows.Forms.Label();
            this.label11 = new System.Windows.Forms.Label();
            this.maskedNumber8 = new MaskedNumber.MaskedNumber();
            this.maskedNumber7 = new MaskedNumber.MaskedNumber();
            this.maskedNumber6 = new MaskedNumber.MaskedNumber();
            this.maskedNumber5 = new MaskedNumber.MaskedNumber();
            this.maskedNumber4 = new MaskedNumber.MaskedNumber();
            this.maskedNumber3 = new MaskedNumber.MaskedNumber();
            this.maskedNumber2 = new MaskedNumber.MaskedNumber();
            this.maskedNumber1 = new MaskedNumber.MaskedNumber();
            this.maskedNumberValor = new MaskedNumber.MaskedNumber();
            this.SuspendLayout();
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(10, 12);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(72, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Formato Valor";
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(10, 36);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(111, 13);
            this.label2.TabIndex = 2;
            this.label2.Text = "Formato Porcentagem";
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(10, 64);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(72, 13);
            this.label3.TabIndex = 4;
            this.label3.Text = "Formato Peso";
            //
            // label4
            //
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(10, 90);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(81, 13);
            this.label4.TabIndex = 6;
            this.label4.Text = "Formato Moeda";
            //
            // label5
            //
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(10, 315);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(73, 13);
            this.label5.TabIndex = 19;
            this.label5.Text = "Digite o Valor:";
            //
            // linkLabel1
            //
            this.linkLabel1.Location = new System.Drawing.Point(7, 372);
            this.linkLabel1.Name = "linkLabel1";
            this.linkLabel1.Size = new System.Drawing.Size(263, 75);
            this.linkLabel1.TabIndex = 21;
            this.linkLabel1.TabStop = true;
            this.linkLabel1.Text = "linkLabel1";
            this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
            //
            // label6
            //
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(63, 266);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(157, 13);
            this.label6.TabIndex = 16;
            this.label6.Text = "Defina o formato para \"Custom\"";
            //
            // label7
            //
            this.label7.AutoSize = true;
            this.label7.Location = new System.Drawing.Point(10, 290);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(48, 13);
            this.label7.TabIndex = 17;
            this.label7.Text = "Formato:";
            //
            // txtFormatoCustom
            //
            this.txtFormatoCustom.Location = new System.Drawing.Point(121, 286);
            this.txtFormatoCustom.Name = "txtFormatoCustom";
            this.txtFormatoCustom.Size = new System.Drawing.Size(150, 20);
            this.txtFormatoCustom.TabIndex = 18;
            this.txtFormatoCustom.Text = "000,00/00-00";
            //
            // label8
            //
            this.label8.AutoSize = true;
            this.label8.Location = new System.Drawing.Point(10, 200);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(68, 13);
            this.label8.TabIndex = 12;
            this.label8.Text = "Formato CPF";
            //
            // label9
            //
            this.label9.AutoSize = true;
            this.label9.Location = new System.Drawing.Point(10, 226);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(75, 13);
            this.label9.TabIndex = 14;
            this.label9.Text = "Formato CNPJ";
            //
            // label10
            //
            this.label10.AutoSize = true;
            this.label10.Location = new System.Drawing.Point(10, 174);
            this.label10.Name = "label10";
            this.label10.Size = new System.Drawing.Size(69, 13);
            this.label10.TabIndex = 10;
            this.label10.Text = "Formato CEP";
            //
            // label11
            //
            this.label11.AutoSize = true;
            this.label11.Location = new System.Drawing.Point(10, 133);
            this.label11.Name = "label11";
            this.label11.Size = new System.Drawing.Size(93, 13);
            this.label11.TabIndex = 8;
            this.label11.Text = "Formato Telefone:";
            //
            // maskedNumber8
            //
            this.maskedNumber8.CustomFormat = "{0:0}";
            this.maskedNumber8.Format = MaskedNumberFormat.Telefone;
            this.maskedNumber8.Location = new System.Drawing.Point(121, 133);
            this.maskedNumber8.Name = "maskedNumber8";
            this.maskedNumber8.Size = new System.Drawing.Size(150, 20);
            this.maskedNumber8.TabIndex = 9;
            this.maskedNumber8.Text = "0800-123-4567";
            this.maskedNumber8.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            //
            // maskedNumber7
            //
            this.maskedNumber7.CustomFormat = "{0:0}";
            this.maskedNumber7.Format = MaskedNumberFormat.CEP;
            this.maskedNumber7.Location = new System.Drawing.Point(121, 174);
            this.maskedNumber7.Name = "maskedNumber7";
            this.maskedNumber7.Size = new System.Drawing.Size(150, 20);
            this.maskedNumber7.TabIndex = 11;
            this.maskedNumber7.Text = "12345-678";
            this.maskedNumber7.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            //
            // maskedNumber6
            //
            this.maskedNumber6.CustomFormat = "{0:0}";
            this.maskedNumber6.Format = MaskedNumberFormat.CNPJ;
            this.maskedNumber6.Location = new System.Drawing.Point(121, 226);
            this.maskedNumber6.Name = "maskedNumber6";
            this.maskedNumber6.Size = new System.Drawing.Size(150, 20);
            this.maskedNumber6.TabIndex = 15;
            this.maskedNumber6.Text = "12.345.678/9012-34";
            this.maskedNumber6.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            //
            // maskedNumber5
            //
            this.maskedNumber5.CustomFormat = "{0:0}";
            this.maskedNumber5.Format = MaskedNumberFormat.CPF;
            this.maskedNumber5.Location = new System.Drawing.Point(121, 200);
            this.maskedNumber5.Name = "maskedNumber5";
            this.maskedNumber5.Size = new System.Drawing.Size(150, 20);
            this.maskedNumber5.TabIndex = 13;
            this.maskedNumber5.Text = "123.456.789-01";
            this.maskedNumber5.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            //
            // maskedNumber4
            //
            this.maskedNumber4.CustomFormat = "000,00/00-00";
            this.maskedNumber4.Format = MaskedNumberFormat.Custom;
            this.maskedNumber4.Location = new System.Drawing.Point(121, 315);
            this.maskedNumber4.Name = "maskedNumber4";
            this.maskedNumber4.Size = new System.Drawing.Size(150, 20);
            this.maskedNumber4.TabIndex = 20;
            this.maskedNumber4.Text = "123.45/67-89";
            this.maskedNumber4.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            this.maskedNumber4.TextChanged += new System.EventHandler(this.maskedNumber4_TextChanged);
            this.maskedNumber4.Enter += new System.EventHandler(this.maskedNumber4_Enter);
            //
            // maskedNumber3
            //
            this.maskedNumber3.CustomFormat = "{0:0}";
            this.maskedNumber3.Format = MaskedNumberFormat.Moeda;
            this.maskedNumber3.Location = new System.Drawing.Point(121, 90);
            this.maskedNumber3.Name = "maskedNumber3";
            this.maskedNumber3.Size = new System.Drawing.Size(150, 20);
            this.maskedNumber3.TabIndex = 7;
            this.maskedNumber3.Text = "R$ 1.253,56";
            this.maskedNumber3.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            //
            // maskedNumber2
            //
            this.maskedNumber2.CustomFormat = "{0:0}";
            this.maskedNumber2.Format = MaskedNumberFormat.Peso;
            this.maskedNumber2.Location = new System.Drawing.Point(121, 64);
            this.maskedNumber2.Name = "maskedNumber2";
            this.maskedNumber2.Size = new System.Drawing.Size(150, 20);
            this.maskedNumber2.TabIndex = 5;
            this.maskedNumber2.Text = "123,566";
            this.maskedNumber2.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            //
            // maskedNumber1
            //
            this.maskedNumber1.CustomFormat = "{0:0}";
            this.maskedNumber1.Format = MaskedNumberFormat.Porcentagem;
            this.maskedNumber1.Location = new System.Drawing.Point(121, 36);
            this.maskedNumber1.Name = "maskedNumber1";
            this.maskedNumber1.Size = new System.Drawing.Size(150, 20);
            this.maskedNumber1.TabIndex = 3;
            this.maskedNumber1.Text = "89,33%";
            this.maskedNumber1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            //
            // maskedNumberValor
            //
            this.maskedNumberValor.CustomFormat = "{0:0}";
            this.maskedNumberValor.Format = MaskedNumberFormat.Valor;
            this.maskedNumberValor.Location = new System.Drawing.Point(121, 12);
            this.maskedNumberValor.Name = "maskedNumberValor";
            this.maskedNumberValor.Size = new System.Drawing.Size(150, 20);
            this.maskedNumberValor.TabIndex = 1;
            this.maskedNumberValor.Text = "34.897,34";
            this.maskedNumberValor.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            //
            // frmMain
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(283, 458);
            this.Controls.Add(this.label11);
            this.Controls.Add(this.maskedNumber8);
            this.Controls.Add(this.label10);
            this.Controls.Add(this.maskedNumber7);
            this.Controls.Add(this.label9);
            this.Controls.Add(this.maskedNumber6);
            this.Controls.Add(this.label8);
            this.Controls.Add(this.maskedNumber5);
            this.Controls.Add(this.txtFormatoCustom);
            this.Controls.Add(this.label7);
            this.Controls.Add(this.label6);
            this.Controls.Add(this.linkLabel1);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.maskedNumber4);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.maskedNumber3);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.maskedNumber2);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.maskedNumber1);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.maskedNumberValor);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "frmMain";
            this.Text = "Exemplo de uso do MaskedNumber";
            this.Load += new System.EventHandler(this.frmMain_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private MaskedNumber.MaskedNumber maskedNumberValor;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private MaskedNumber.MaskedNumber maskedNumber1;
        private System.Windows.Forms.Label label3;
        private MaskedNumber.MaskedNumber maskedNumber2;
        private System.Windows.Forms.Label label4;
        private MaskedNumber.MaskedNumber maskedNumber3;
        private System.Windows.Forms.Label label5;
        private MaskedNumber.MaskedNumber maskedNumber4;
        private System.Windows.Forms.LinkLabel linkLabel1;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.Label label7;
        private System.Windows.Forms.TextBox txtFormatoCustom;
        private System.Windows.Forms.Label label8;
        private MaskedNumber.MaskedNumber maskedNumber5;
        private System.Windows.Forms.Label label9;
        private MaskedNumber.MaskedNumber maskedNumber6;
        private System.Windows.Forms.Label label10;
        private MaskedNumber.MaskedNumber maskedNumber7;
        private System.Windows.Forms.Label label11;
        private MaskedNumber.MaskedNumber maskedNumber8;
        #endregion
    }
}

E para quem não quiser copiar e colar o código todo. Ai vai o link da solução para download.
Download: Masked Number em C# (1507)

É isso ai pessoal 🙂
Até o próximo
♦ Marcelo

Compartilhe!

About Marcelo

Nascido em Juruaia/MG em uma fazenda de criação de búfalos, e residindo na região Sul do Brasil. Trabalha com desenvolvimento de aplicações desde os 17 anos. Atualmente é Arquiteto Organizacional na Unimake Software. Para saber mais ... http://desenvolvedores.net/marcelo []'s

Deixe uma resposta

O seu endereço de e-mail não será publicado.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>