简体   繁体   中英

How to make a textbox only accep a-z and nothing else?

I have 4 text boxes at a Windows form. I would like to change it to only accept letters the letters a to z and nothing else, even when content is pasted in. If the user pastes in a mix of letters and unwanted characters, only the letters should show up in the textbox.

The last thing that I would like to have is the numlock pad. Those numbers are the same as the number row at a top of a keyboard, but I want to them to block them too!

I am pretty sure there should be some syntax that looks like; var isAlpha = char.IsLetter('text'); All you need to do is implement the syntax to your textbox, as shown; var isAlpha = textbox.char.IsLetter('text');

In the ASCII table az is form 97 to 122:

string str = "a string with some CAP letters and 123numbers";
void Start(){
    string result = KeepaToz(str);
    Debug.Log(result); // print "astringwithsomelettersandnumbers"
}
string KeepaToz(string input){
   StringBuilder sb = new StringBuilder();
   foreach(char c in str){
       if(c >= 97 && c<= 122){ sb.Append(c); }
   }
   return sb.ToString();
}

I'd suppose to introduce a custom control derived from TextBox , by analogy with this post :

public class LettersTextBox : TextBox
{
    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        base.OnKeyPress(e);

        string c = e.KeyChar.ToString();

        if (e.KeyChar >= 'a' && e.KeyChar <= 'z' || char.IsControl(e.KeyChar))
            return;

        e.Handled = true;
    }

    protected override void WndProc(ref System.Windows.Forms.Message m)
    {
        const int WM_PASTE = 0x0302;
        if (m.Msg == WM_PASTE)
        {
            string text = Clipboard.GetText();
            if (string.IsNullOrEmpty(text))
                return;

            if (text.Any(c => c < 'a' || c > 'z'))
            {
                if (text.Any(c => c >= 'a' || c <= 'z'))
                    SelectedText = new string(text.Where(c => c >= 'a' && c <= 'z').ToArray());
                return;
            }
        }
        base.WndProc(ref m);
    }
}

Use the TextChanged event. Something like:

// Set which characters you allow here
private bool IsCharAllowed(char c)
{
    return (c >= 'a' && c <= 'z')
}    

private bool _parsingText = false;
private void textBox1_TextChanged(object sender, EventArgs e)
{
    // if we changed the text from within this event, don't do anything
    if(_parsingText) return;

    var textBox = sender as TextBox;
    if(textBox == null) return;

    // if the string contains any not allowed characters
    if(textBox.Text.Any(x => !IsCharAllowed(x))
    {        
      // make sure we don't reenter this when changing the textbox's text
      _parsingText = true;
      // create a new string with only the allowed chars
      textBox.Text = new string(textBox.Text.Where(IsCharAllowed).ToArray());         
      _parsingText = false;
    }
}

You can assign this method to each of the textboxes TextChanged events, and they will only allow them to enter what is in IsCharAllowed() (doesn't matter if via pasting, via typing, touchscreen or whatever)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM