Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
This code make my texbox control only can get input char captal 'A'

C#
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = (e.KeyChar == (char)Keys.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

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


anybody can help me?
Posted

You can do something like below :

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


C#
//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');
    }
}


C#
//Handle if list contains key char
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    e.Handled = lstdisableChar.Contains(e.KeyChar);
}
 
Share this answer
 
v2
Comments
Gun Gun Febrianza 18-May-13 3:54am    
sir how to make this code become a method like this

private void disableA()
{
e.Handled = (e.KeyChar == (char)Keys.A)
}

so i can call it another time? but the code in above is not working,..
Mayur Panchal 18-May-13 4:00am    
But why you want to call another method ? you can make a condition in KeyPress event though.
What's your condition, when u want to disable 'A' and 'a' ?
Gun Gun Febrianza 18-May-13 4:08am    
private void disableA()
{
e.Handled = (e.KeyChar == (char)Keys.A)
}

private void disableB()
{
e.Handled = (e.KeyChar == (char)Keys.B)
}

private void disableC()
{
e.Handled = (e.KeyChar == (char)Keys.C)
}

private void disableD()
{
e.Handled = (e.KeyChar == (char)Keys.D)
}

so in another time i can call the method using checkbox,
if checkbox1.checked = true
{
disableA();
}

if checkbox2.checked = true
{
disableB();
}

can you help me?
Mayur Panchal 18-May-13 4:57am    
it's not possible to set e.Handled on checkbox's checkedchanged event.
You have to do some other workaround for that.
I suggest you to maintain disable chars in List<char> when you check on checkboxes and then handle it in keypress event. You need to find in list whether your disable char is present or not.
Mayur Panchal 18-May-13 5:12am    
Look at the updated solution above.
Hi,

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

with this:
C#
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = (e.KeyChar == 'a');
}

Hope this helps.
 
Share this answer
 
Comments
Gun Gun Febrianza 19-May-13 8:16am    
thankyou this is work for me :)
Thomas Daniels 19-May-13 10:26am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900