文本框显示格式

发布于 2024-12-08 13:39:02 字数 170 浏览 1 评论 0原文

我想在每组 3 位数字后面添加“,”。例如:当我输入 3000000 时,文本框将显示 3,000,000,但值仍然是 3000000。
我尝试使用 maskedtexbox,有一个缺点,maskedtexbox 显示类似 _,__,__ 的数字。

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 maskedtexbox, there is a drawback that the maskedtexbox displayed a number like _,__,__ .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

还如梦归 2024-12-15 13:39:02

尝试将此代码添加到 TextBoxKeyUp 事件处理程序中

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

是的,它将更改存储在文本框中的值,但是每当您需要实际数字时,您可以使用以下行从文本中获取:

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

当然不要忘记检查用户在文本框中输入的内容实际上是否是有效的整数。

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.

︶ ̄淡然 2024-12-15 13:39:02

使用 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

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

忆梦 2024-12-15 13:39:02

我希望这可能适合您的场景。

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

在这里,myTxt = 你的文本框。按照下面给出的方式设置 Textchanged 事件,并按照帖子中的内容创建属性文本。

希望有帮助。

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.

殊姿 2024-12-15 13:39:02

您可以像这样连接到 OnKeyUp 事件:

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

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;
                }
            }
        }
鱼窥荷 2024-12-15 13:39:02

获取十进制值然后设置

DecimalValue.ToString("#,#");

Get Decimal Value Then set

DecimalValue.ToString("#,#");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文