C# 输入框组件如何实现自动产生千分位的逗号 比如输入1234556,自动变成1,234,556,实在输入过程中自动变,不是在焦点转移的时候变比如1234,自动变成1,234继续输入12345,自动变成12,345 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <html><body><input id="test" onblur="asfmanFormat(this);" /><script>function asfmanFormat(n) { var o=n; n=n.value.split(".");var str=""; for(var i=0,l=n.length;i<l;i++){ if(i==1) str += "." + n[i].replace(/,/g,"").split("").reverse().join("").replace(/(.{3})/g,"$1,").replace(/,$/,"").split("").reverse().join(""); else str += n[i].replace(/,/g,"").split("").reverse().join("").replace(/(.{3})/g,"$1,").replace(/,$/,"").split("").reverse().join(""); }o.value = strreturn str;} </script></body></html> 我可能没说清楚,我指的是winform应用程序 //只能输入数字private void textBox1_KeyPress(object sender, KeyPressEventArgs e){ string inputText = textBox1.Text.Replace(",", "") + e.KeyChar; e.Handled = e.KeyChar == (char)8 ? false : !Regex.IsMatch(inputText, @"^[1-9]\d*$");}//格式化输入private void textBox1_KeyUp(object sender, KeyEventArgs e){ string inputText = textBox1.Text.Replace(",", ""); if (inputText.Length > 3) { textBox1.Text = double.Parse(inputText).ToString("0,000"); textBox1.SelectionStart = textBox1.Text.Length; }} 核心代码就是在textbox输入框改变时候double text = double.Parse(test.Text);test.Text = String.Format("{0:N2}", text);但是要做很多判断 楼主自己写下吧 不合理的UI设计,会造成操作人员混淆。看下MaskedTextBox:http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.aspx 看这个如何: private void textBox1_TextChanged(object sender, EventArgs e) { int mL = textBox1.SelectionStart; string oldStr = textBox1.Text; if (oldStr == null || oldStr.Length == 0) return; double text = double.Parse(textBox1.Text); string newStr = String.Format("{0:N0}", text); textBox1.Text = newStr; textBox1.SelectionStart = mL + newStr.Length - oldStr.Length; } private void textBox1_TextChanged(object sender, EventArgs e){ try { string inputText = textBox1.Text.Replace(",", ""); if (inputText.Length > 3) { textBox1.Text = double.Parse(inputText).ToString("0,000"); textBox1.SelectionStart = textBox1.Text.Length; } else { if (inputText != "") { textBox1.Text = double.Parse(inputText).ToString("0"); textBox1.SelectionStart = textBox1.Text.Length; } } } catch { MessageBox.Show("格式不正确"); textBox1.Text = ""; }} 正则判断一个 大于0的数(精确到整数4位,小数6位) C#怎么调用 sql数据表设计 的那个窗口 comboBox初始化 C#转ASP.net容易么 combobox添加项问题 请问在Windows应用程序中如何去访问已知站点? 关于ref的使用!! 请问事件可以作为方法的参数吗? 文本框如何显示图片 饶恕我问这么简单的问题,窗体最大化后,再双击标题栏复原,它怎就记不住原来所处的位置? 第一次写程序 编译错误。。。 求教 无从下手啊 MVVM下的WPF自定义控件
<html>
<body>
<input id="test" onblur="asfmanFormat(this);" />
<script>
function asfmanFormat(n)
{
var o=n;
n=n.value.split(".");
var str="";
for(var i=0,l=n.length;i<l;i++)
{
if(i==1)
str += "." + n[i].replace(/,/g,"").split("").reverse().join("").replace(/(.{3})/g,"$1,").replace(/,$/,"").split("").reverse().join("");
else
str += n[i].replace(/,/g,"").split("").reverse().join("").replace(/(.{3})/g,"$1,").replace(/,$/,"").split("").reverse().join("");
}
o.value = str
return str;
}
</script>
</body>
</html>
//只能输入数字
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
string inputText = textBox1.Text.Replace(",", "") + e.KeyChar;
e.Handled = e.KeyChar == (char)8 ? false : !Regex.IsMatch(inputText, @"^[1-9]\d*$");
}
//格式化输入
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
string inputText = textBox1.Text.Replace(",", "");
if (inputText.Length > 3)
{
textBox1.Text = double.Parse(inputText).ToString("0,000");
textBox1.SelectionStart = textBox1.Text.Length;
}
}
double text = double.Parse(test.Text);
test.Text = String.Format("{0:N2}", text);但是要做很多判断 楼主自己写下吧
看下MaskedTextBox:
http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.aspx
private void textBox1_TextChanged(object sender, EventArgs e)
{
int mL = textBox1.SelectionStart;
string oldStr = textBox1.Text;
if (oldStr == null || oldStr.Length == 0)
return;
double text = double.Parse(textBox1.Text);
string newStr = String.Format("{0:N0}", text);
textBox1.Text = newStr;
textBox1.SelectionStart = mL + newStr.Length - oldStr.Length;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
try
{
string inputText = textBox1.Text.Replace(",", "");
if (inputText.Length > 3)
{
textBox1.Text = double.Parse(inputText).ToString("0,000");
textBox1.SelectionStart = textBox1.Text.Length;
}
else
{
if (inputText != "")
{
textBox1.Text = double.Parse(inputText).ToString("0");
textBox1.SelectionStart = textBox1.Text.Length;
} }
}
catch
{
MessageBox.Show("格式不正确");
textBox1.Text = "";
}}