怎样在文本框中只能输入汉字? 怎样在文本框中只能输入汉字? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 下面的代码是判断数字(是汉字和数字时,把判断条件改一下就可以。)代码来自http://bingning.net/VB/SOURCE/control/numerictextbox.htmlpublic class MyTextBox : TextBox { const int WM_PASTE = 0x302; protected override void WndProc(ref Message m) { if (m.Msg == WM_PASTE) { IDataObject iData = Clipboard.GetDataObject(); //检测剪贴板是否有字符串 if (iData.GetDataPresent(DataFormats.Text)) { string clipStr = (string) iData.GetData(DataFormats.Text); //检测剪贴板的字符串是文字还是数字 if (!System.Text.RegularExpressions.Regex.IsMatch( clipStr, @"^\d+$")) return; } } base.WndProc(ref m); } } /// <summary> /// 判断是否是中文字符 /// </summary> /// <param name="poChar">传入的字符</param> /// <returns></returns> public static bool IsChinese(char poChar) { if ((poChar > '\u4e00') && (poChar < '\u9fa5')) { return true; } return false; } /[^\u4E00-\u9FA5]/g 这个 正解:将文本框的ImeMode属性设置为Disabled if (Regex.IsMatch(Temp,@"[\u4e00-\u9fa5]")) { TextBox1.Text+=Temp; } public class MyTextBox : TextBox { const int WM_PASTE = 0x302; protected override void WndProc(ref Message m) { if (m.Msg == WM_PASTE) { IDataObject iData = Clipboard.GetDataObject(); //检测剪贴板是否有字符串 if (iData.GetDataPresent(DataFormats.Text)) { string clipStr = (string) iData.GetData(DataFormats.Text); //检测剪贴板的字符串是文字还是数字 if (!System.Text.RegularExpressions.Regex.IsMatch( clipStr, @"^[\u4e00-\u9fa5]*$")) //改了 ^[\u4e00-\u9fa5]*$ return; } } base.WndProc(ref m); } } 将文本框的ImeMode属性设置为Disabled 这个只是屏蔽你不能输入汉字,我的目的是只允许输入汉字!如果用正则表达式的话,可以, if(m.Msg == WM_PASTE) 这是什么意思啊?各位大哥,还有没有简单一点的方法 我想提醒一句,一般用的判定中文unicode范围只是个大致,因为这个范围包含了其它东亚国家字体,如果说只要区分出中英文,用上面说的正则表达^[\u4e00-\u9fa5]*即可。 public static bool IsChinese(char poChar) { //判断是否是中文的字符 if ((poChar > '\u4e00') && (poChar < '\u9fa5')) { return true; } return false; } 简单的正则问题 C#求XML文件节点数据的最大值 使用DataView的RowFilter过滤数据 如何取动态的值.listView1 关于把flash图片存到数据库里在取出来的问题。。help me 顶的都有分 whidbey的正式版什么时候出来啊? 怎样在C#中实现VS.NET中调试信息窗口的自动滚动功能? 新手再问--数据点画波形图问题 类的成员函数调用问题 请问各位,在哪里可以下载vs.net? 远程执行SQL脚本的问题 帮人一次,胜造七级浮屠。高手看看这个程序哪里错了?在线等,感激不尽
代码来自http://bingning.net/VB/SOURCE/control/numerictextbox.htmlpublic class MyTextBox : TextBox
{
const int WM_PASTE = 0x302; protected override void WndProc(ref Message m)
{
if (m.Msg == WM_PASTE)
{
IDataObject iData = Clipboard.GetDataObject();
//检测剪贴板是否有字符串
if (iData.GetDataPresent(DataFormats.Text))
{
string clipStr = (string) iData.GetData(DataFormats.Text);
//检测剪贴板的字符串是文字还是数字
if (!System.Text.RegularExpressions.Regex.IsMatch(
clipStr,
@"^\d+$"))
return;
}
}
base.WndProc(ref m);
}
}
/// 判断是否是中文字符
/// </summary>
/// <param name="poChar">传入的字符</param>
/// <returns></returns>
public static bool IsChinese(char poChar)
{
if ((poChar > '\u4e00') && (poChar < '\u9fa5'))
{
return true;
}
return false;
}
{
TextBox1.Text+=Temp;
}
{
const int WM_PASTE = 0x302; protected override void WndProc(ref Message m)
{
if (m.Msg == WM_PASTE)
{
IDataObject iData = Clipboard.GetDataObject();
//检测剪贴板是否有字符串
if (iData.GetDataPresent(DataFormats.Text))
{
string clipStr = (string) iData.GetData(DataFormats.Text);
//检测剪贴板的字符串是文字还是数字
if (!System.Text.RegularExpressions.Regex.IsMatch(
clipStr,
@"^[\u4e00-\u9fa5]*$")) //改了 ^[\u4e00-\u9fa5]*$
return;
}
}
base.WndProc(ref m);
}
}
这个只是屏蔽你不能输入汉字,我的目的是只允许输入汉字!
如果用正则表达式的话,可以,
if(m.Msg == WM_PASTE) 这是什么意思啊?各位大哥,还有没有简单一点的方法
{
//判断是否是中文的字符
if ((poChar > '\u4e00') && (poChar < '\u9fa5'))
{
return true;
}
return false;
}