Formatting TextBox for thousands?

2

I'm trying to create a TextBox to enter values in thousands, the values will be for weights (kilos). I want to enter the values from right to left based on the 0,000 mask and as the user enters the weight the values are being overwritten in the zeros ... For example: 0,000 typing 1 will get 0,001 and so on , if the user erases the 1 the mask returns to 0,000 as the default.

I found an example to do this that works great, but the class is for monetary values and I am trying to adapt to use weights but I am not able to adapt.

How to do this?

Here is the Currency class I am using as an example to adapt the Weight class.

public class Moeda  {
        private TextBox txtBox;
        private String txt = string.Empty;
        private Decimal valor = 0;

        public Moeda(TextBox txtBox) {
            this.txtBox = txtBox;
            this.txtBox.RightToLeft = RightToLeft.Yes;
            this.txtBox.Text = "0,00";
            this.txtBox.KeyPress += keyPress;
            this.txtBox.TextChanged += new EventHandler(textChanged);            
            this.txtBox.Font = new Font(this.txtBox.Font, FontStyle.Bold);            
        }

        private void textChanged(object obj, EventArgs e) {
            try {
                txt = txtBox.Text.Replace(",", "").Replace(".", "");
                if (txt.Equals("")) {
                    txtBox.Text = "0,00";
                }
                txt = txt.PadLeft(3, '0');                
                if (txt.Length > 3 & txt.Substring(0, 1) == "0")
                    txt = txt.Substring(1, txt.Length - 1);
                valor = Convert.ToDecimal(txt) / 100;
                txtBox.Text = string.Format("{0:N}", valor);
                txtBox.SelectionStart = txtBox.Text.Length;
            }catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }            
        }

        private void keyPress(object obj, KeyPressEventArgs e) {
            if (!(Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar))) { e.Handled = true; }
        }
    }

Here is the Weight class I'm trying to adapt based on the Currency class.

public class Peso  {
        private TextBox txtBox;
        private String txt = string.Empty;
        private Decimal valor = 0;

        public Peso(TextBox txtBox) {
            this.txtBox = txtBox;
            this.txtBox.RightToLeft = RightToLeft.Yes;             
            this.txtBox.Text = "0,000";
            this.txtBox.KeyPress += keyPress;
            this.txtBox.TextChanged += new EventHandler(textChanged);           
            this.txtBox.Font = new Font(this.txtBox.Font, FontStyle.Bold);            
        }

        private void textChanged(object obj, EventArgs e) {
            try {
                txt = txtBox.Text.Replace(",", "").Replace(".", "");
                if (txt.Equals("")) {
                    txtBox.Text = "0,000";
                }
                txt = txt.PadLeft(4, '0');
                if (txt.Length > 4 & txt.Substring(0, 1) == "0")
                    txt = txt.Substring(1, txt.Length - 1);
                valor = Convert.ToDecimal(txt);
                txtBox.Text = string.Format("{0:N}", valor);
                txtBox.SelectionStart = txtBox.Text.Length;
            }catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }       
        }


        private void keyPress(object obj, KeyPressEventArgs e) {
            if (!(Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar))) { e.Handled = true; }
        }


    }
    
asked by anonymous 06.04.2016 / 15:12

1 answer

3

Friend, I found that dividing the "value" by 1000, results in the format with 3 zeros before the comma as you wish. Modify this line:

valor = Convert.ToDecimal(txt); 

with this value:

valor = Convert.ToDecimal(txt) / 1000; 

and complements this way:

txtBox.Text = String.Format("{0:N3}", valor);

I hope it helps.

    
07.04.2016 / 03:24