textBox和keypress 我想问的是在textBox框中输入非0-9数字时,textBox自动忽略掉,例如说我在输入123后再按个K进去,但不充许K显示,怎么搞? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private void textBox1_KeyPress(object sender, KeyPressEventArgs e){ if (e.KeyChar < '0' || e.KeyChar > '9') { e.Handled = true; }} winform or webformwinform下private void textBox1_TextChanged(object sender, EventArgs e){ textBox1.Text = System .Text .RegularExpressions.Regex.Replace(textBox1.Text, @"[^0-9\.]", "");}另参考http://community.csdn.net/Expert/topic/5386/5386616.xml?temp=.9118158 给您一个“限制文本框的输入”的例子:1、在 Microsoft Visual Studio .NET 的“文件”菜单上,单击“新建”,然后单击“项目”。 2、在“新建项目”对话框中,选择“Visual C#项目”,再选择“Windows 应用程序”模板。 3、键入 TextApp 作为该程序的名称,然后单击确定。4、在Form1.cs的视图设计器中,添加如下控件:控件类型 控件名称 控件属性 属性值Label label1 Text 输入十进制数Label label2 Text 输入大写字母TextBox textBox1TextBox textBox25、在Form1.cs的视图设计器中,选中textBox1,在属性框中选中事件,双击KeyPress,在Form1.cs的代码设计器中,添加修改如下代码private void textBox1_KeyPress(object sender, KeyPressEventArgs e){ if (Char.IsNumber(e.KeyChar) || (Keys)e.KeyChar == Keys.Back || e.KeyChar.ToString() == ".") e.Handled = false;//允许输入 else e.Handled = true;//取消输入}6、在Form1.cs的视图设计器中,选中textBox2,在属性框中选中事件,双击KeyPress,在Form1.cs的代码设计器中,添加修改如下代码private void textBox2_KeyPress(object sender, KeyPressEventArgs e){ if (Char.IsUpper(e.KeyChar) || (Keys)e.KeyChar == Keys.Back) e.Handled = false;//允许输入 else e.Handled = true;//取消输入}7、编译要想轻松开发软件,详见http://www.psec.net.cn 不好意思Snowdust(雪尘)兄弟,因为你的解说没那么清楚,所以我把10分发给cq_lqj(程序员秘书) 了. 超菜的问题! c#里面的Byte和byte有什么区别? 求像QQ那样窗体在屏幕边缘就隐藏 代码 在C#中是否可以使用类似VB中的InputBox? 关于Microsoft Visual SourceSafe项目数据自动备份的解决方案 关于菜单项应该非常简单的问题(20分) C#如何实现窗体加载完毕后自动加载数据(类似PB中的PostEvent) IIS+ASP.NET WEB服务器的负载问题 大虾请进,多线程调用问题 大家 给我一份 数据库 相关 榜定 grid 的例子 [email protected] 如何从一个窗口获取另一个窗口里面的公共变量值! c# word操作问题
{
if (e.KeyChar < '0' || e.KeyChar > '9')
{
e.Handled = true;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Text = System .Text .RegularExpressions.Regex.Replace(textBox1.Text, @"[^0-9\.]", "");
}另参考
http://community.csdn.net/Expert/topic/5386/5386616.xml?temp=.9118158
1、在 Microsoft Visual Studio .NET 的“文件”菜单上,单击“新建”,然后单击“项目”。
2、在“新建项目”对话框中,选择“Visual C#项目”,再选择“Windows 应用程序”模板。
3、键入 TextApp 作为该程序的名称,然后单击确定。
4、在Form1.cs的视图设计器中,添加如下控件:
控件类型 控件名称 控件属性 属性值
Label label1 Text 输入十进制数
Label label2 Text 输入大写字母
TextBox textBox1
TextBox textBox2
5、在Form1.cs的视图设计器中,选中textBox1,在属性框中选中事件,双击KeyPress,在Form1.cs的代码设计器中,添加修改如下代码
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (Char.IsNumber(e.KeyChar) || (Keys)e.KeyChar == Keys.Back || e.KeyChar.ToString() == ".")
e.Handled = false;//允许输入
else
e.Handled = true;//取消输入
}
6、在Form1.cs的视图设计器中,选中textBox2,在属性框中选中事件,双击KeyPress,在Form1.cs的代码设计器中,添加修改如下代码
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (Char.IsUpper(e.KeyChar) || (Keys)e.KeyChar == Keys.Back)
e.Handled = false;//允许输入
else
e.Handled = true;//取消输入
}
7、编译
要想轻松开发软件,详见http://www.psec.net.cn