65.9K
CodeProject is changing. Read more.
Home

Formatting your numbers

starIconstarIconemptyStarIconemptyStarIconemptyStarIcon

2.00/5 (3 votes)

Oct 21, 2002

viewsIcon

117465

downloadIcon

132

Now it is simple to format your numbers.

Sample Image - format.jpg

Step 1

Add the source code in WebControl Library Project.

Step 2

Analize the source code.

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

//your personal namespace
namespace Personal.Web.UI.WebControls
{
    //WebCustom Control 
    //inherits System.Web.UI.WebControls.TextBox (Framework)
    public class TextBoxMask :System.Web.UI.WebControls.TextBox 
    {
        public TextBoxMask()
        {
        }

        #region Event TextChanged

        //override to event TextChanged 
        //Using PostBack
        protected override void OnTextChanged(EventArgs e)
        {
            TextChange(this,e);
        }

        //function configure your format
        public virtual void TextChange(object sender,EventArgs e)
        {
            string txt = "";

            if (this.Text.Trim() != "")
            {
                double val = double.Parse(this.ParserValue(this.Text));
                txt = this.Format(val,this.ImputMask);
            }

            this.Text = txt;

            if (this.ReleaseMask)
            {
                this.ImputMask = "";
            }

            base.OnTextChanged(e);
        }


        #endregion

        //render this object TextBoxMask
        protected override void Render(HtmlTextWriter output)
        {
            this.AutoPostBack = true;
            base.Render(output);
        }

        //format to using double.ToString()
        protected string Format(double number,string mask)
        {
            string ret = "";
            ret = number.ToString(mask);

            return ret;
        }

        //define to value is only numeric
        protected string ParserValue(string Value)
        {
            int i=0;
            string val = "";
            string ret = "";

            for (i=0;i<Value.Length;i++)
            {
                val = Value.Substring(i,1); 

                if (char.IsDigit(Value,i))
                {
                    ret += val;
                }

            }

            return ret;
        }

        //Your Mask 
        //$#,##0.00;($#,##0.00);Zero Monetary
        private string imputmask;
        public virtual string ImputMask
        {
            set
            {
                imputmask = value;
            }
            get
            {
                return imputmask; 
            }
        }

        //The value without it masks 
        public string ClipText
        {
            get
            {
                if (this.Text != null || this.Text.Trim() !="")
                {
                    return this.ParserValue(this.Text);  
                }
                else
                {
                    return "";
                }
            }
        }

    }
}

Last Step

  1. Compile.
  2. Customize Toolbox and add reference to assembly.
  3. Drag-Drop the TextboxMask control to page.
  4. Run the application.

Something appears similar to the figure.