c尖锐的Windows应用程序,文本框也应像2.3一样接受 [英] c sharp windows application ,the textbox should accepts like 2.3 likewise

查看:42
本文介绍了c尖锐的Windows应用程序,文本框也应像2.3一样接受的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文本框应像2.3一样接受

the textbox should accepts like 2.3 likewise

推荐答案

TextBox 控件的KeyDown 事件可用于此目的.按下0 to 9 .back space以外的键时,SuppressKeyPress 属性设置为True,这将阻止按下的键传递给TextBox,如下所示
The KeyDown event of TextBox control can be used for this purpose. When a key other than 0 to 9, . and back space is pressed the SuppressKeyPress property is set to True, which blocks the key pressed being passed to the TextBox, as shown below
void Main()
{
    Form form1 = new Form();
    TextBox textBox1 = new TextBox();
    textBox1.KeyDown += (sender, args) => {
    	if(args.KeyCode==Keys.Back || args.KeyCode==Keys.OemPeriod) return;
    	 
    	if((args.KeyValue < (int)'0' || args.KeyValue > (int)'9'))
    		args.SuppressKeyPress = true;
    	
    };
    form1.Controls.Add(textBox1);
    form1.ShowDialog();
}


但是最好使用MaskedTextBox ,这在这里进行了解释,
http://msdn.microsoft.com/en-us/library/system. windows.forms.maskedtextbox.aspx [ ^ ]
出于指定的目的.


But it is preferable to use MaskedTextBox , which is explained here,
http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.aspx[^]
for the purpose specified.


这篇关于c尖锐的Windows应用程序,文本框也应像2.3一样接受的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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