我必须在当前窗口设置中检测小数分隔符.我使用visual studio 2010,windows窗体.特别是,如果DecimalSeparator是逗号,如果用户在textbox1中输入点,我需要在textbox2中显示零.
我尝试使用此代码,但不起作用:
private void tbxDaConvertire_KeyPress(object sender, KeyPressEventArgs e)
{
string uiSep = CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator;
if (uiSep.Equals(","))
{
while (e.KeyChar == (char)46)
{
tbxConvertito.Text = "0";
}
}
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过这段代码,但不能正常工作:
private void tbxDaConvertire_KeyPress(object sender, KeyPressEventArgs e)
{
string uiSep = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
if (uiSep.Equals(","))
{
if (e.KeyChar == (char)46)
{
tbxConvertito.Text = "0";
}
}
}
Run Code Online (Sandbox Code Playgroud)
And*_*res 59
解:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
char a = Convert.ToChar(Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator);
if (e.KeyChar == a)
{
e.Handled = true;
textBox1.Text = "0";
}
}
Run Code Online (Sandbox Code Playgroud)
希望有所帮助.这样,当你点击"." 或","你的文本框为0
编辑:
如果你想在每次点击小数分隔符时插入0,这就是代码:
char a = Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
if (e.KeyChar == a)
{
e.KeyChar = '0';
}
Run Code Online (Sandbox Code Playgroud)
小智 28
其实你应该使用
Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator
Run Code Online (Sandbox Code Playgroud)
代替
CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator
Run Code Online (Sandbox Code Playgroud)
使用第二个为您提供操作系统默认设置,这可能与登录到此PC的特定用户帐户的用户区域设置不同
| 归档时间: |
|
| 查看次数: |
67383 次 |
| 最近记录: |