Format Textbox for thousands?

Asked

Viewed 643 times

2

I’m trying to create a Textbox to report values in thousands, the values will be for weights(kilos). I want to type the values from right to left based on the mask 0,000 and as the user type the weight the values are overwritten in the zeros... For example: 0,000 typing the 1 will stay 0,001 and so successively, if the user deletes the 1 the mask returns to 0,000 as in standard.

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

How to do this ?

Here’s the Currency class I’m 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’s 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; }
        }


    }
  • What is your difficulty?

  • @bigown if you can, please test the 2 classes for you to see how they work. The Weight class does not work as it should. To test simply create a Textbox in the Form and Load Form new Peso(SeuTextBox)

  • It is not easier for you to explain what the expected behavior is and what the current behavior is?

  • @jbueno but I already reported in the post the expected behavior. I want to go typing the numbers from 0 to 9 to replace the 0 with them, as in a bank teller. In the Currency class this happens, if you can test, please see that it is so.

  • @Fernandopaiva I have nothing installed here, so I can not test, anyway, it is not for us to test, for you to say what is the problem.

  • @mustache if there’s no way you can test and I have no way to tell you what’s wrong in a way that’s clear and you understand, I made a video to show you what’s going on. See: https://youtu.be/361vOJ5F1KQ

Show 1 more comment

1 answer

3


Friend, I discovered that if you divide the "value" by 1000, it results in the format with 3 zeros before the comma as you want. Modifies this line:

valor = Convert.ToDecimal(txt); 

at this value:

valor = Convert.ToDecimal(txt) / 1000; 

and thus complements:

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

I hope it helps.

Browser other questions tagged

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