Textbox display formatting

前端 未结 5 1338
花落未央
花落未央 2020-12-19 21:30

I want to add \",\" to after every group of 3 digits. Eg : when I type 3000000 the textbox will display 3,000,000 but the value still is 3000000.
I tried to use maskedte

相关标签:
5条回答
  • 2020-12-19 21:35

    Try adding this code to KeyUp event handler of your TextBox

    private void textBox1_KeyUp(object sender, KeyEventArgs e)
    {
        if (!string.IsNullOrEmpty(textBox1.Text))
        {
            System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US");
            int valueBefore = Int32.Parse(textBox1.Text, System.Globalization.NumberStyles.AllowThousands);
            textBox1.Text = String.Format(culture, "{0:N0}", valueBefore);
            textBox1.Select(textBox1.Text.Length, 0);
        }
    }
    

    Yes, it will change the value stored in a texbox, but whenever you need the actual number you can use the following line to get it from the text:

    int integerValue = Int32.Parse(textBox1.Text, System.Globalization.NumberStyles.AllowThousands);
    

    Of course do not forget to check that what the user inputs into the textbox is actually a valid integer number.

    0 讨论(0)
  • 2020-12-19 21:41

    You could hook up to OnKeyUp event like this:

     private void textBox1_KeyUp(object sender, KeyEventArgs e)
            {
                if (!(e.KeyCode == Keys.Back))
                {
                    string text = textBox1.Text.Replace(",", "");
                    if (text.Length % 3 == 0)
                    {
                        textBox1.Text += ",";
                        textBox1.SelectionStart = textBox1.Text.Length;
                    }
                }
            }
    
    0 讨论(0)
  • 2020-12-19 21:42

    This may work fine for your scenario I hope.

     private string text
            {
                get
                {
                    return text;
                }
                set
                {
                    try
                    {
                        string temp = string.Empty;
                        for (int i = 0; i < value.Length; i++)
                        {
                            int p = (int)value[i];
                            if (p >= 48 && p <= 57)
                            {
                                temp += value[i];
                            }
                        }
                        value = temp;
                        myTxt.Text = value;
                    }
                    catch
                    { 
    
                    }
                }
            }
    
        private void digitTextBox1_TextChanged(object sender, EventArgs e)
        {
            if (myTxt.Text == "")
                return;
            int n = myTxt.SelectionStart;
            decimal text = Convert.ToDecimal(myTxt.Text);
            myTxt.Text = String.Format("{0:#,###0}", text);
            myTxt.SelectionStart = n + 1;
        }
    

    Here, myTxt = your Textbox. Set Textchanged event as given below and create a property text as in the post.

    Hope it helps.

    0 讨论(0)
  • 2020-12-19 21:55

    Get Decimal Value Then set

    DecimalValue.ToString("#,#");
    
    0 讨论(0)
  • 2020-12-19 21:57

    Use String.Format

    int value = 300000
    String.Format("{0:#,###0}", value);
    // will return 300,000
    

    http://msdn.microsoft.com/en-us/library/system.string.format.aspx

    0 讨论(0)
提交回复
热议问题