动态数字分组 [英] Dynamic digit grouping

查看:75
本文介绍了动态数字分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个很严重的问题.我希望我的数字在输入时立即分组.我使用了如下的数字格式

Hi everyone,

I have a very serious problem. I want my digits to be in groups in my textbox as soon as I type them. I used number format like below

if (textBox1.Text != "")
{
  NumberFormatInfo nFI = new CultureInfo("en-US",false).NumberFormat; 

  double int_value = Convert.ToDouble(textBox1.Text);
  textBox1.Text=int_value.ToString("N5", nFI);          
}


但是当我在文本框中编写此代码时出现问题(按键事件).
在某个数字之后,一些数字从文本的左侧插入,而另一些数字从右侧插入,但是如果我在将整个文本写在文本框中后在butten click事件中编写代码,则它会起作用.
实际上,在文本框中输入整个单词后,我就可以进行分组.

请帮助我:((:((:((X |


but there is a problem when I write this code in the text box (key down event).
After some digit, some digit is inserted from left of text and some others from right but if I write the code in a butten click event after writing the whole text in text box it works.
In fact I can just have grouping after writing whole word in my textbox.

Help me please :(( :(( :(( X|

推荐答案

您不想使用KeyDown事件-您应该使用TextChanged事件.

You don''t want to use the KeyDown event - you should use the TextChanged event.


我刚刚写了一个小胜表应用程序来理解您的问题,得出的结论是,当您设置文本框的Text属性时,当前插入符号的位置将丢失.您可以事先阅读插入符号的位置,然后在将重新格式化的字符串写入文本框后,将插入符号的位置重新设置为它的位置.

干杯,

Manfred
From a small win form app I just wrote to understand your problem, I came to the conclusion that when you set the Text property of the textbox, the current caret position is lost. You can read the caret position beforehand and after you write your reformatted string to the textbox set the caret position back to where it belongs.

Cheers,

Manfred


您是否尝试过使用KeyPress事件?

您还需要添加一些验证,如果文本框中出现非数字,则Convert.ToDouble(textBox1.Text);将引发异常.
Have you tried using the KeyPress event?

You also need to add some validation, Convert.ToDouble(textBox1.Text); will throw an exception if the a non-digit is in the textbox.


这篇关于动态数字分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆