有关TextBox中输入字符控制的一种解决办法
来源:互联网 发布:中韩进出口贸易数据 编辑:程序博客网 时间:2024/06/05 02:28
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
我们知道,在C#中,TextBox控件对输入字符的控制有keypress、keyup、和keydown事件来使用,但大家也看到了,这几个事件对输入字符的控制都有一定的缺陷,如果,你使用中文输入法,那么很多原来你不希望输入的字符也可以输入进去。这几天做程序的时候,就碰到了这个问题,我的解决思路很简单,既然这三个事件不再起作用,那么我就使用了TextBox控件中时刻能发生的TextChanged事件,以期望在这个事件中作些东西,以达到控制字符的目的。废话就不多说了,我将控制输入字符为数字的代码粘贴出来,希望大家多指正,如果能对你有益的话,我就更happy了。
private System.Windows.Forms.TextBox TextBox1;
private string text;
public Form1()
{
text = TextBox1.Text;
}
private void TextBox1_TextChanged(object sender, System.EventArgs e)
{
int len = text.Length;
if(len < TextBox1.Text.Length)
{
int index = TextBox1.Text.IndexOf(text);
char c = (TextBox1.Text.Remove(index,text.Length))[0];
if(c.CompareTo('0') < 0||c.CompareTo('9') >0)
{
TextBox1.Text = text;
TextBox1.SelectionStart = text.Length;
}
}
text = TextBox1.Text;
}
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- 有关TextBox中输入字符控制的一种解决办法
- 有关TextBox中输入字符控制的一种解决办法
- 有关TextBox中输入字符控制的一种解决办法
- 有关TextBox中输入字符控制的一种解决办法
- 有关TextBox中输入字符控制的一种解决办法
- 控制TextBox框的输入字符
- 控制TextBox控件只输入数字的解决办法
- C#的winform中控制TextBox中只能输入数字?
- HTML中textbox的长度随着输入字符而变长
- 控制C#中textbox输入文本的格式
- TextBox属性TextMode="MultiLine"后字符输入长度控制的方法。
- 可以屏蔽某些输入字符的TextBox
- .net 限制textbox的输入字符长度
- 关于控制textbox的输入问题
- TextBox输入控制代码
- C#的winform中控制TextBox中只能输入正整数,首位不能为0
- textBox中输入的字符为数值型 非正则方法
- 如何实现在TextBox控件中输入的密码显示为字符*?
- 在asp.net中为Web用户控件添加属性和事件
- 追踪ASP.NET代码里的bug
- ASP.NET对IIS中的虚拟目录进行操作
- 关于ASP.NET在IIS一些问题的经验总结
- ASP.NET访问Oracle数据库的方法
- 有关TextBox中输入字符控制的一种解决办法
- ASP.NET中利用cookies保持客户端信息
- ASP.NET下MVC设计模式的实现
- 使用更精简的代码保证ASP.NET应用程序的安全
- 为ASP.NET应用缓存Oracle数据
- ASP.NET保持用户状态的九种选择(下)
- ASP.NET保持用户状态的九种选择(上)
- ASP.NET中编程杀死进程
- ASPX页Web服务调用性能优化