如何在文本框中输入数字后自动显示带千位分隔符的格式 在文本框中输入数字,动态的能判断有几位数字,如果输入第四个数字的时候能自动在文本框中显示千位分隔符,输入第七个数字的时候能自动显示第二个千位分隔符.类推.希望能在文本框中动态显示... 谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 帮输入的数值转换成字符串,然后用ToString("###,###") 的方式转化一下就可以了! 如何在textBox1_TextChanged 这样一个事件中去实现输入数字自动出现千位分隔符呢... 如果是整数的话,用toint32就行了textBox1.Text = Convert.ToDecimal(textBox1.Text).ToString("n2"); yuzhlhua 那个... 我一输入数字自动会出现小数点以及后面两位,如:5.00,然后光标会自动跳到数字的最前面 实在不好意思了,想不到了!如果不要求,可以在光标离开的时候在转化.tostring("n2") n 表示有千位分隔符 2表示小数的位数. private void textBox1_TextChanged(object sender, System.EventArgs e){ CultureInfo ci=(CultureInfo)Thread.CurrentThread.CurrentCulture.Clone(); NumberFormatInfo nfi=ci.NumberFormat; nfi.CurrencyPositivePattern=1; nfi.CurrencyDecimalDigits=0; nfi.CurrencyGroupSeparator=","; nfi.CurrencySymbol=""; ci.NumberFormat=nfi; Thread.CurrentThread.CurrentCulture=ci; //以上这些可不写到此事件中 如load里 string str=textBox1.Text; textBox1.Text=Decimal.Parse(textBox1.Text.Trim()).ToString("c"); textBox1.SelectionStart=textBox1.Text.Length;} 一种方法给TextBox添加2种状态,显示状态和编辑状态在显示状态下:TextBox的值表示为格式化值在编辑状态下:TextBox的值表示为正常数字值/////////////////////////////////////////////////////////////////public enum TextBoxMode { //显示模式 Display, //编辑模式 Edit};public class TextBoxPlus : System.Windows.Forms.TextBox{ #region 变量 private double m_Value; private TextBoxMode m_Mode; #endregion #region 属性 private string EditText { get { if (m_Mode != TextBoxMode.Edit) { throw new Exception("在非编辑模式下不能取得EditText值"); } return this.Text; } set { if (m_Mode != TextBoxMode.Edit) { throw new Exception("在非编辑模式下不能设置EditText值"); } this.TextChanged -= new EventHandler(TextChangedHandler); this.Text = value; this.TextChanged += new EventHandler(TextChangedHandler); } } private string DisplayText { get { if (m_Mode != TextBoxMode.Display) { throw new Exception("在非显示模式下不能取得DisplayText值"); } return this.Text; } set { if (m_Mode != TextBoxMode.Display) { throw new Exception("在非显示模式下不能设置DisplayText值"); } this.TextChanged -= new EventHandler(TextChangedHandler); this.Text = value; this.TextChanged += new EventHandler(TextChangedHandler); } } public TextBoxMode Mode { get { return m_Mode; } set { if (m_Mode != value) { m_Mode = value; OnModeChanged(); } } } #endregion #region 构造器 public TextBoxPlus() : base() { m_Mode = TextBoxMode.Display; this.TextAlign = HorizontalAlignment.Right; this.TextChanged += new EventHandler(TextChangedHandler); } #endregion //TextChanged事件处理 private void TextChangedHandler(object sender, System.EventArgs e) { if (m_Mode == TextBoxMode.Display) { SetDisplayTextToValue(); } else { SetEditTextToValue(); } } private void SetValueToDisplayText() { //////////////////////////////////////////////// //在这里加上数字格式化的代码 //例:把数字格式化为保留小数点3位显示 this.DisplayText = m_Value.ToString("0.000"); ///////////////////////////////////////////////// } private void SetValueToEditText() { this.EditText = m_Value.ToString(); } private void SetEditTextToValue() { try { m_Value = double.Parse(this.EditText); } catch(Exception) { } } private void SetDisplayTextToValue() { throw new Exception("不能在显示值模式下修改Value值"); } //模式变换事件处理 private void OnModeChanged() { if (m_Mode == TextBoxMode.Display) { SetValueToDisplayText(); } else { SetValueToEditText(); } } //只能输入数字 protected override void OnKeyPress(KeyPressEventArgs e) { base.OnKeyPress (e); switch(e.KeyChar) { case '1': return; case '2': return; case '3': return; case '4': return; case '5': return; case '6': return; case '7': return; case '8': return; case '9': return; case '0': return; case '.': if (this.Text.IndexOf('.') != -1) { e.Handled = true; } return; case (char)Keys.Back : return; case (char)Keys.Left : return; case (char)Keys.Right : return; default : e.Handled = true; return; } } protected override void OnLeave(System.EventArgs e) { base.OnLeave(e); this.Mode = TextBoxMode.Display; } protected override void OnGotFocus(System.EventArgs e) { base.OnGotFocus(e); this.Mode = TextBoxMode.Edit; }} c#一道面试题 怎么实现按一下退格键能删除多个字符串? 如何打开一个新的form 想制作安装程序,没有做过,帮忙(100分不够再加) 动态创建控件? 如何使用水晶报表???救命 救命:如何在项目中禁用windows的密码记忆功能! 如何做出像Visual Studio .NET 自带的组件一样的组件!!!!!! 如何将存储过程返回的三个select结果同时fill到dataset中啊?不一样的问题 如何使应用程序能够象QQ一样 急~~~~!水晶报表如何显示2张无关联的表?~ 在这一瞬间,终于又升到5叉了,散分啦
textBox1.Text = Convert.ToDecimal(textBox1.Text).ToString("n2");
如果不要求,可以在光标离开的时候在转化.
tostring("n2") n 表示有千位分隔符 2表示小数的位数.
{
CultureInfo ci=(CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
NumberFormatInfo nfi=ci.NumberFormat;
nfi.CurrencyPositivePattern=1;
nfi.CurrencyDecimalDigits=0;
nfi.CurrencyGroupSeparator=",";
nfi.CurrencySymbol="";
ci.NumberFormat=nfi;
Thread.CurrentThread.CurrentCulture=ci;
//以上这些可不写到此事件中 如load里
string str=textBox1.Text;
textBox1.Text=Decimal.Parse(textBox1.Text.Trim()).ToString("c");
textBox1.SelectionStart=textBox1.Text.Length;
}
给TextBox添加2种状态,显示状态和编辑状态
在显示状态下:TextBox的值表示为格式化值
在编辑状态下:TextBox的值表示为正常数字值
/////////////////////////////////////////////////////////////////
public enum TextBoxMode {
//显示模式
Display,
//编辑模式
Edit};public class TextBoxPlus : System.Windows.Forms.TextBox
{
#region 变量
private double m_Value;
private TextBoxMode m_Mode;
#endregion #region 属性
private string EditText
{
get
{
if (m_Mode != TextBoxMode.Edit)
{
throw new Exception("在非编辑模式下不能取得EditText值");
}
return this.Text;
}
set
{
if (m_Mode != TextBoxMode.Edit)
{
throw new Exception("在非编辑模式下不能设置EditText值");
} this.TextChanged -= new EventHandler(TextChangedHandler); this.Text = value; this.TextChanged += new EventHandler(TextChangedHandler);
}
} private string DisplayText
{
get
{
if (m_Mode != TextBoxMode.Display)
{
throw new Exception("在非显示模式下不能取得DisplayText值");
} return this.Text;
}
set
{
if (m_Mode != TextBoxMode.Display)
{
throw new Exception("在非显示模式下不能设置DisplayText值");
}
this.TextChanged -= new EventHandler(TextChangedHandler);
this.Text = value;
this.TextChanged += new EventHandler(TextChangedHandler);
}
} public TextBoxMode Mode
{
get
{
return m_Mode;
}
set
{
if (m_Mode != value)
{
m_Mode = value;
OnModeChanged();
}
}
}
#endregion #region 构造器
public TextBoxPlus() : base()
{
m_Mode = TextBoxMode.Display;
this.TextAlign = HorizontalAlignment.Right;
this.TextChanged += new EventHandler(TextChangedHandler);
}
#endregion //TextChanged事件处理
private void TextChangedHandler(object sender, System.EventArgs e)
{
if (m_Mode == TextBoxMode.Display)
{
SetDisplayTextToValue();
}
else
{
SetEditTextToValue();
}
} private void SetValueToDisplayText()
{
////////////////////////////////////////////////
//在这里加上数字格式化的代码
//例:把数字格式化为保留小数点3位显示
this.DisplayText = m_Value.ToString("0.000");
/////////////////////////////////////////////////
} private void SetValueToEditText()
{
this.EditText = m_Value.ToString();
} private void SetEditTextToValue()
{
try
{
m_Value = double.Parse(this.EditText);
}
catch(Exception)
{
}
} private void SetDisplayTextToValue()
{
throw new Exception("不能在显示值模式下修改Value值");
} //模式变换事件处理
private void OnModeChanged()
{
if (m_Mode == TextBoxMode.Display)
{
SetValueToDisplayText();
}
else
{
SetValueToEditText();
}
} //只能输入数字
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress (e);
switch(e.KeyChar)
{
case '1': return;
case '2': return;
case '3': return;
case '4': return;
case '5': return;
case '6': return;
case '7': return;
case '8': return;
case '9': return;
case '0': return;
case '.':
if (this.Text.IndexOf('.') != -1)
{
e.Handled = true;
}
return;
case (char)Keys.Back : return;
case (char)Keys.Left : return;
case (char)Keys.Right : return;
default : e.Handled = true;
return;
}
} protected override void OnLeave(System.EventArgs e)
{
base.OnLeave(e);
this.Mode = TextBoxMode.Display;
} protected override void OnGotFocus(System.EventArgs e)
{
base.OnGotFocus(e);
this.Mode = TextBoxMode.Edit;
}
}