使用numericUpDown更改文本框值中的小数位

时间:2013-01-19 18:17:28

标签: c# winforms textbox numericupdown

使用此代码,我可以使用numericUpDown修改小数位。如果我initialize myDecimal 变量,此代码有效。但我需要将decimal位置修改为textbox中输入的值。

换言之myDecimal = tbxConvertito.Text。但在这种情况下,代码不起作用。

请检查此页面中的屏幕截图: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;
    }
}

2 个答案:

答案 0 :(得分:0)

您可以从Text拆分属性tbxConvertito

同样,当TextBox中的文本发生更改时,您必须调用numericUpDown1_ValueChanged来限制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);
        }
    }

没有丢失数据:

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;
        }
    }

方案:

  1. [小数位数= 0]您可以输入例如[1],[1234],[12345]
  2. [小数位数= 1]您可以输入例如[1,1],[1234],[12345,3]
    如果 您将NumericUpDown中的值更改为0,显示的值将为[1], [1234],[12345]
    如果您再次将NumericUpDown中的值更改为1 显示的值将是[1,1],[1234],[12345,3]
  3. [小数位数= 1]与第2步的情况相同
  4. 现在我们不会丢失任何数据。只有当用户在我们的文本框中输入内容时,我们才会更新原始数据。

答案 1 :(得分:0)

试一试

    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(",", "");
        }

    }