[英]Check if string is a specific character, and limit it to one character
I'm making a calculator for my C# class.我正在为我的 C# class 制作计算器。 I'm not allowed to use buttons for operands.
我不允许对操作数使用按钮。 There's an operand text box in which the user is supposed to enter "+" for addition, "-" for subtraction, "/" for division, or "*" for multiplication.
有一个操作数文本框,用户应该在其中输入“+”表示加法,“-”表示减法,“/”表示除法,或“*”表示乘法。
I'm trying to find out how to make it where you can only enter one of those four characters, but it also only lets you input one at a time.我试图找出如何使它只能输入这四个字符之一,但它也只能让你一次输入一个。
I'm pretty new to C#.我对 C# 很陌生。
Thank you to "TheGeneral" for helping me figure this out, by giving me this link .感谢“TheGeneral”通过给我这个链接帮助我解决这个问题。
I'm not sure why I couldn't find this question on Google.我不确定为什么我在 Google 上找不到这个问题。
This is how I did it:我是这样做的:
private void TxtOperator_TextChanged(object sender, EventArgs e)
{
Regex regex = new Regex(@"[^+^\-^\/^\*^\(^\)]");
MatchCollection matches = regex.Matches(txtOperator.Text);
if (matches.Count > 0)
{
MessageBox.Show("Please input an operator.");
txtOperator.Text = "";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.