Click here to Skip to main content
12,064,782 members (30,791 online)
Click here to Skip to main content
Add your own
alternative version

Stats

109K views
101 downloads
21 bookmarked
Posted

Formatting your numbers

, 21 Oct 2002
Rate this:
Please Sign up or sign in to vote.
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Esteves
Web Developer
Brazil Brazil
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionHow do I get a similar format in Winforms Pin
Guru2kGh2-Mar-03 21:52
memberGuru2kGh2-Mar-03 21:52 
GeneralNeed help on how to convert the format Pin
Guru2kGh1-Mar-03 13:31
memberGuru2kGh1-Mar-03 13:31 
GeneralHorrible switch case stuff... Pin
__Stephane Rodriguez__21-Oct-02 11:39
member__Stephane Rodriguez__21-Oct-02 11:39 
GeneralRe: Horrible switch case stuff... Pin
Esteves21-Oct-02 14:13
memberEsteves21-Oct-02 14:13 
GeneralRe: Horrible switch case stuff... Pin
Paul Selormey21-Oct-02 19:14
memberPaul Selormey21-Oct-02 19:14 
GeneralRe: Horrible switch case stuff... Pin
Esteves21-Oct-02 19:23
memberEsteves21-Oct-02 19:23 
GeneralRe: Horrible switch case stuff... Pin
Daniel Turini21-Oct-02 23:36
memberDaniel Turini21-Oct-02 23:36 
GeneralRe: Horrible switch case stuff... Pin
Esteves22-Oct-02 3:05
memberEsteves22-Oct-02 3:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.160204.4 | Last Updated 22 Oct 2002
Article Copyright 2002 by Esteves
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid