限制文本框控件中的字符 [英] Limit Char in Textbox Control

查看:62
本文介绍了限制文本框控件中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码使我的texbox控件只能输入char captal''A''



This code make my texbox control only can get input char captal ''A''

private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = (e.KeyChar == (char)Keys.A)
}





但是如何使我的文本框控件只能在这段代码下输入char小''a''

是不行的,我找不到任何想法或任何解决它的技巧





but how to make my textbox control only can get input char small ''a''
below this code is not work,i can''t find any idea or any trick to solve it

private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = (e.KeyChar == (char)Keys.a)
}





任何人都可以帮助我?



anybody can help me?

推荐答案

你可以做如下的事情:



You can do something like below :

//Define a list of char, which will store chars to be disabled
List<char> lstdisableChar = new List<char>();







//Assign below event for all the checkboxes, here you have store char in above defined list
//on checked and remove on  uncheck.
private void CheckBox_CheckedChanged(object sender, EventArgs e)
{
    CheckBox chkTmp = sender as CheckBox;

    if (chkTmp.Name == checkBox1.Name)
    {
        if (chkTmp.Checked)
            lstdisableChar.Add('A');
        else 
            lstdisableChar.Remove('A');
    }
    else if (chkTmp.Name == checkBox2.Name)
    {
        if (chkTmp.Checked)
            lstdisableChar.Add('a');
        else
            lstdisableChar.Remove('a');
    }
}







//Handle if list contains key char
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    e.Handled = lstdisableChar.Contains(e.KeyChar);
}






而不是使用枚举,使用 char 。因此,替换:

Hi,

Instead of using the Keys enum, use char. So, replace:
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = (e.KeyChar == (char)Keys.a)
}



这个:


with this:

private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = (e.KeyChar == 'a');
}



希望这有帮助。


Hope this helps.


这篇关于限制文本框控件中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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