[英]Clearing out a c# form text box
我正在以C#格式制作一个简单的子手风格的游戏。 我将其设置为textBox1_TextChanged。 除非用户每次都将其猜测字母都退格,否则它将占用空白空间。 在消息说对/错后清除空格后,我该怎么做。 我很生气,告诉用户在退格后他们做出了错误的猜测。 这是我在该论坛上的第一篇文章,如果代码文本很奇怪,请对不起。 我只希望程序在他们猜测之后清除textBox中的文本。
更新:添加了建议的信息。 现在,它可以完成它应该做的所有事情。 除了弹出一个窗口,说“在目标词中发现”。 如果guessLetter == null ||,则会发生这种情况。 guessLetter ==正确|| guessLetter ==否。
private void textBox1_TextChanged(object sender, EventArgs e)
{
string guessLetter = textBox1.Text;
//textBox1.ReadOnly = true;
if (targetWord == null)
{
MessageBox.Show("Please start a new game.");
textBox1.Text = ("");
}
else
{
if (targetWord.Contains(guessLetter))
{
MessageBox.Show(guessLetter + " was found in the word");
}
else
{
MessageBox.Show(guessLetter + " was not found in the word");
incorrectGuessCtr++;
textBox3.Text = incorrectGuessCtr.ToString();
}
textBox1.Text = ("");
}
}
不仅要检查targetWord
是否为null,还要检查guessLetter
。 您最好也使用string.IsNullOrEmpty
,因为它还会检查字符串是否为空:
if (!string.IsNullOrEmpty(targetWord) && !string.IsNullOrEmpty(guessLetter))
{
...
}
我想您还应该检查是否输入了恰好一个字母。 那将意味着额外的检查:
if (guessLetter.Length == 1)
{
...
}
当您编写更改文本框中的Text属性的代码时,将输入此事件。 我是说这个
textBox3.Text = incorrectGuessCtr.ToString();
在函数参数中放置一些内容或设置一些标志,以便您可以标识是从用户输入中调用事件还是清除文本。 只需检查用户按下退格键时调用此函数的次数即可。 您会明白的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.