C# Winform TextBox控件多行输入
C#winform对控件textbox输入文本的限制
对于textbox的输入进行不同情况的限制(举例)
总结
C# Winform TextBox控件多行输入TextBox控件默认是单行输入。怎么才能进行多行输入呢。
只需要将控件属性MultiLine由false改为true即可。
C#winform对控件textbox输入文本的限制textbox的输入限制
对于textbox的输入进行不同情况的限制(举例)1.只能输入数字
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
//只允许输入数字
if(!(char.IsNumber(e.KeyChar)||e.KeyChar=='b'))//Char.IsNumber()方法用于表示指定的Unicode字符是否被归类为数字。
{ //'b'是退格键
e.Handled = true;
}
}
2.只能输入数字跟小数点
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 46)
e.Handled = true;//小数点得处理
if(e.KeyChar==46)//小数点
{
if(textBox_price.Text.Length<=0)
{
e.Handled = true;//小数点不能在第一位
}
else
{
float f;
float oldf;
bool b1 = false, b2 = false;
b1 = float.TryParse(textBox_price.Text, out oldf);
b2 = float.TryParse(textBox_price.Text + e.KeyChar.ToString(), out oldf);
if(b2==false)
{
if(b1==true)
{
e.Handled = true;
}
else
{
e.Handled = false;
}
}
}
}
}
3.只能输入数字跟指定字母‘X’
private void textBox_idcar_KeyPress(object sender, KeyPressEventArgs e)
{
//只允许输入数字跟字母‘X'
if((e.KeyChar<48||e.KeyChar>57)&&(e.KeyChar!=8)&&e.KeyChar!='X')
{
e.Handled = true;
}
}
4.只允许输入汉字
using System.Text.RegularExpressions;//提供正则表达式功能
private void textBox_name_KeyPress(object sender, KeyPressEventArgs e)
{
Regex rg = new Regex("^[\u4e00-\u9fa5]$");//正则表达式
if(!rg.IsMatch(e.KeyChar.ToString())&&e.KeyChar!='\b')
{
e.Handled = true;
}
}
对textbox的ImeMode属性进行设置也能限制其输入方式
总结以上为个人经验,希望能给大家一个参考,也希望大家多多支持软件开发网。