开发者

Textbox display formatting

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 maskedt开发者_Go百科exbox, there is a drawback that the maskedtexbox displayed a number like _,__,__ .


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.


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


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.


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


Get Decimal Value Then set

DecimalValue.ToString("#,#");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜