Click here to Skip to main content
14,775,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I'm currently having a little problem. I have a winforms project and want to restrict the use of the characters in certain textboxes to letters and backspaces only.

Using the code example below, it does detect the numbers and correctly empties the field but there are two problems I'm running into:

1. The MessageBox shows twice, once when it detects the number and once when it empties the textbox

2. Backspaces are not allowed, even if I add \b

What I have tried:

private void textBox_Ansprechpartner_Name_TextChanged(object sender, EventArgs e)
    if (!System.Text.RegularExpressions.Regex.IsMatch(textBox_Ansprechpartner_Name.Text, "^[a-zA-Z ]"))
        MessageBox.Show("This textbox may only contain letters.");
        textBox_Ansprechpartner_Name.Text = string.Empty;
Updated 18-Apr-18 0:44am

1. The MessageBox is shown twice because TextChanged is triggered when you assign string.Empty. You can temporarily unassign the TextChanged handler before clearing and then restore it.

2. In your example, you can press the backspace key, which will remove the text to the left of the cursor and then trigger TextChanged again. If it's not empty it must've been valid before, and by removing a character it should still be.
Could you clarify the problem maybe?
Firstly, that's a nasty bit of UI - clearing out everything because he made a minor slip is rather unpleasant, and second because it won't work as is.
If I type "A123345!"£$%^&*(" is will accept it, because it starts with a valid character.

You can fix it so it works and only accepts letters and spaces very easily:
if (!System.Text.RegularExpressions.Regex.IsMatch(textBox_Ansprechpartner_Name.Text, "^[a-zA-Z ]*$"))
But I'd look at saving the previous version:
private string lastEntry = "";
private void textBox_Ansprechpartner_Name_TextChanged(object sender, EventArgs e)
    if (!System.Text.RegularExpressions.Regex.IsMatch(textBox_Ansprechpartner_Name.Text, "^[a-zA-Z ]*$"))
        int loc = textBox_Ansprechpartner_Name.SelectionStart;
        MessageBox.Show("This textbox may only contain letters.");
        textBox_Ansprechpartner_Name.Text = lastEntry;
        textBox_Ansprechpartner_Name.SelectionStart = loc;
    lastEntry = textBox_Ansprechpartner_Name.Text;
Member 13777741 18-Apr-18 5:55am
THank you very much for your input!
It does make sense to not delete everything when one slips, though what you suggested has the same problem, if I start with a letter I can input everything I want after that. How do I work around that?
OriginalGriff 18-Apr-18 5:57am
Check the regex, and compare it to the one in my code...
Member 13777741 18-Apr-18 6:02am
I have both changed mine and blatantly copy-pasted yours - to see if I made a mistake - and the result is the same for both-
OriginalGriff 18-Apr-18 6:11am
That's odd - when I test it, I type "abc1" and get the MessageBox on the '1' and nowhere else.

Check it compiles cleanly - do a full rebuild and check the "Error List" pane to make sure there are no errors - and then use the debugger to check it executes the right code.
Member 13777741 18-Apr-18 6:16am
Nevermind, I'm just really, really stupid. Your code works just fine, excuse me.
OriginalGriff 18-Apr-18 6:30am
We all have days like that! :laugh:

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