简体   繁体   English

允许用户只输入文字?

[英]Allow user to only input text?

how do I make a boolean statement to only allow text? 如何使布尔语句只允许文本? I have this code for only allowing the user to input numbers but ca'nt figure out how to do text. 我有这个代码只允许用户输入数字,但不知道如何做文本。

 bool Dest = double.TryParse(this.xTripDestinationTextBox.Text, out Miles);
 bool MilesGal = double.TryParse(this.xTripMpgTextBox.Text, out Mpg);
 bool PriceGal = double.TryParse(this.xTripPricepgTextBox.Text, out Price);

Update: looking at your comment I would advise you to read this article: 更新:看你的评论我建议你阅读这篇文章:

User Input Validation in Windows Forms Windows窗体中的用户输入验证


Original answer: The simplest way, at least if you are using .NET 3.5, is to use LINQ: 原始答案:至少在使用.NET 3.5时,最简单的方法是使用LINQ:

bool isAllLetters = s.All(c => char.IsLetter(c));

In older .NET versions you can create a method to do this for you: 在较旧的.NET版本中,您可以创建一个方法来为您执行此操作:

bool isAllLetters(string s)
{
    foreach (char c in s)
    {
        if (!char.IsLetter(c))
        {
            return false;
        }
    }
    return true;
}

You can also use a regular expression. 您还可以使用正则表达式。 If you want to allow any letter as defined in Unicode then you can use this regular expression: 如果要允许Unicode中定义的任何字母,则可以使用此正则表达式:

bool isOnlyLetters = Regex.IsMatch(s, @"^\p{L}+$");

If you want to restrict to AZ then you can use this: 如果您想限制为AZ,那么您可以使用:

bool isOnlyLetters = Regex.IsMatch(s, @"^[a-zA-Z]+$");

您可以使用正则表达式,在此处浏览: http//regexlib.com/

You can do one of a few things. 你可以做一些事情。

  1. User Regular Expression to check that the string matches your concept of 'Text' only 用户正则表达式,用于检查字符串是否仅与“文本”概念匹配
  2. Write some code that checks each character against a list of valid characters. 编写一些代码,根据有效字符列表检查每个字符。 Or even use char.IsLetter() 甚至使用char.IsLetter()
  3. Use the MaskedTextBox and set a custom mask to limit input to text only characters 使用MaskedTextBox并设置自定义蒙版以限制输入到仅文本字符

You can use following code in the KeyPress event: 您可以在KeyPress事件中使用以下代码:

if (!char.IsLetter(e.KeyChar)) {
e.Handled = true;
}

I found hooking up into the text changed event of a textbox and accepting/rejecting the changes an acceptable solution. 我发现连接到文本框的文本更改事件并接受/拒绝更改是可接受的解决方案。 To "only allow text" is a rather vague definition, but you may as well check if your newly added text (the next char) is a number or not and simply reject all numbers/disallowed characters. “仅允许文本”是一个相当模糊的定义,但您也可以检查新添加的文本(下一个字符)是否为数字,并简单地拒绝所有数字/不允许的字符。 This will make users feel they can only enter characters and special characters (like dots, question marks etc.). 这将使用户感觉他们只能输入字符和特殊字符(如点,问号等)。

private void UTextBox_TextChanged(object sender, EventArgs e)
{
    string lastCharacter = this.Text[this.Text.Length-1].ToString();
    MatchCollection matches = Regex.Matches(lastCharacter, "[0-9]", RegexOptions.None);
    if (matches.Count > 0) //character is a number, reject it.
    {
         this.Text = Text.Substring(0, Text.Length-1);
    }
 }

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

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