سؤال

With this code I can modify decimal place, using numericUpDown. This code works if I initialize myDecimal variable. But I need modify decimal place to a value typed in textbox.

In other word myDecimal = tbxConvertito.Text. But in this case, code not work.

Check screenshot in this page please: change decimal place in textbox using numericUpDown

public partial class Form1 : Form
{
    public decimal myDecimal = 3755.25012345M;

    public Form1()
    {
        InitializeComponent();
        tbxConvertito.Text = myDecimal.ToString();
        numericUpDown1_ValueChanged(this, EventArgs.Empty);
    }

    private void numericUpDown1_ValueChanged(object sender, EventArgs e)
    {          
        int decimalPlace = (int)numericUpDown1.Value;
        string[] numbers = myDecimal.ToString().Split(new char[] { '.', ',' });
        string tmp = string.Empty;
        if (decimalPlace <= numbers[1].Length)
        {
            tmp = "," + numbers[1].Substring(0, decimalPlace);

            if (tmp.EndsWith(","))
                tmp = string.Empty;
        }
        else
            tmp = "," + numbers[1];

        tbxConvertito.Text = numbers[0] + tmp;
    }
}
هل كانت مفيدة؟

المحلول

You can split property Text from tbxConvertito.

Also when text will be changed in TextBox you must invoke numericUpDown1_ValueChanged to restrict settings in NumericUpDown.

 public partial class Form1 : Form
    {
        public decimal myDecimal = 3755.25012345M;

        public Form1()
        {
            InitializeComponent();
            tbxConvertito.Text = myDecimal.ToString();                        
            numericUpDown1_ValueChanged(this, EventArgs.Empty);
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {          
            int decimalPlace = (int)numericUpDown1.Value;                        

            string[] numbers = tbxConvertito.Text.Split(new char[] { '.', ',' });

            string tmp = string.Empty;
            if (numbers.Length != 1)
            {
                if (decimalPlace <= numbers[1].Length)
                {
                    tmp = "," + numbers[1].Substring(0, decimalPlace);

                    if (tmp.EndsWith(","))
                        tmp = string.Empty;
                }
                else
                    tmp = "," + numbers[1];                
            }
            tbxConvertito.Text = numbers[0] + tmp; 
            tbxConvertito.Select(tbxConvertito.Text.Length, 0);
        }

        private void tbxConvertito_TextChanged(object sender, EventArgs e)
        {
            numericUpDown1_ValueChanged(this, EventArgs.Empty);
            decimal.TryParse(tbxConvertito.Text.Replace(',', '.'), out myDecimal);
        }
    }

Without lose data:

public partial class Form1 : Form
    {
        public decimal myDecimal = 0;

        public Form1()
        {
            InitializeComponent();
            // init value
            tbxConvertito.Text = myDecimal.ToString();
            numericUpDown1_ValueChanged(this, EventArgs.Empty);
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            int decimalPlace = (int)numericUpDown1.Value;

            string[] numbers = myDecimal.ToString().Split(new char[] { '.', ',' });

            string tmp = string.Empty;
            if (numbers.Length != 1)
            {
                if (decimalPlace <= numbers[1].Length)
                {
                    tmp = "," + numbers[1].Substring(0, decimalPlace);

                    if (tmp.EndsWith(","))
                        tmp = string.Empty;
                }
                else
                    tmp = "," + numbers[1];
            }
            tbxConvertito.Text = numbers[0] + tmp;

            tbxConvertito.Select(tbxConvertito.Text.Length, 0);
        }

        private void tbxConvertito_TextChanged(object sender, EventArgs e)
        {
            if (keyValue == 188) return;
            if (keyPressed)
            {
                string stringValue = tbxConvertito.Text;
                if ((stringValue.Contains(',') && stringValue.Split(new char[] { ',' })[1].Length <= (int)numericUpDown1.Value) || !stringValue.Contains(','))
                    decimal.TryParse(tbxConvertito.Text.Replace(',', '.'), out myDecimal);
                keyPressed = false;
            }
            numericUpDown1_ValueChanged(this, EventArgs.Empty);
            Console.WriteLine("Displayed value: {0}", tbxConvertito.Text);
            Console.WriteLine("Actual value: {0}", myDecimal);
        }

        bool keyPressed = false;
        int keyValue;

        private void tbxConvertito_KeyDown(object sender, KeyEventArgs e)
        {
            keyValue = e.KeyValue;
            keyPressed = true;
        }
    }

Scenarios:

  1. [Decimal Places = 0] you can type e.g. [1], [1234], [12345]
  2. [Decimal Places = 1] you can type e.g. [1,1], [1234], [12345,3]
    if you change value in NumericUpDown to 0 displayed value will be [1], [1234], [12345]
    if you change value in NumericUpDown to 1 again displayed value will be [1,1], [1234], [12345,3]
  3. [Decimal Places = 1] the same situation as for step 2

Now we don't lose any data. We update our original data only if user will type something in our textbox.

نصائح أخرى

Give it a try

    private void numericUpDown1_ValueChanged(object sender, EventArgs e)
    {
        //var ix = tbxConvertito.Text.IndexOf(',');
        decimal myDecimal = 0;
        bool IsDecimal = decimal.TryParse(tbxConvertito.Text, out myDecimal);
        if (IsDecimal)
        {
            decimal letDivide = myDecimal / 100;
            int decimalPlace = (int)numericUpDown1.Value;
            tbxConvertito.Text = letDivide.ToString().Replace(".", "");

            var index = tbxConvertito.Text.Length - decimalPlace;
            if (index > -1)
                tbxConvertito.Text = tbxConvertito.Text.Insert(index, ",");
            else
                tbxConvertito.Text = tbxConvertito.Text.Insert(1, ",");
        }
        else
        {
            tbxConvertito.Text = tbxConvertito.Text.ToString().Replace(",", "");
        }

    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top