简体   繁体   English

如何使文本框只接受az而不是其他?

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

I have 4 text boxes at a Windows form. 我在Windows窗体上有4个文本框。 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. 我想将其更改为仅接受字母a到z的字母,而不是其他内容,即使粘贴内容也是如此。如果用户粘贴了字母和不需要的字符混合,则只有字母应显示在文本框中。

The last thing that I would like to have is the numlock pad. 我想要的最后一件事是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: 在ASCII表中,az的格式为97到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 : 我想通过类比这篇文章来介绍一个从TextBox派生的自定义控件:

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. 使用TextChanged事件。 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) 您可以将此方法分配给每个文本框TextChanged事件,并且它们只允许它们输入IsCharAllowed() (如果通过粘贴,通过键入,触摸屏或其他方式无关紧要)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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