2 回答

TA贡献1876条经验 获得超5个赞
您的代码的问题是您如何定义if语句:
if (textBox1.Text == "") ;
最后;的 表示如果条件true现在完成则要执行的代码。我不确定这是否可以编译,但是如果可以,则您基本上没有任何操作。
if 语句可以用两种方式编写:
if (textBox1.Text == "")
{
MessageBox.Show("abc");
// you can place as much code inside the block as you like, and it will only be executed if the condition is true
}
在这种风格中,if语句将执行其{ ... }下方的封闭块 ( )。或者,您可以这样编写if语句:
if (textBox1.Text == "")
MessageBox.Show("abc"); // this can be on the same line as the if statement.
MessageBox.Show("def"); // this line is not part of the if statement and will always execute regardless of the condition being met
在这种风格中,if语句在满足条件时执行单行代码。请注意,defMessageBox 不是if语句的一部分,因此它将始终被执行。
所以,我们应该这样写你的代码:
if (textBox1.Text == "Aim4last")
{
Main temp = new Main();
temp.Region = this.Region;
temp.Show();
this.Hide();
}
else if (textBox1.Text == "")
{
MessageBox.Show("Incorrect Key");
}
这导致了一个新问题:我们只有两个条件,"Aim4last"或""。文本框可以包含其他值,但不会在键错误时生成消息框。要解决此问题,请将其更改为 anelse而不是else if:
if (textBox1.Text == "Aim4last")
{
Main temp = new Main();
temp.Region = this.Region;
temp.Show();
this.Hide();
}
else
{
MessageBox.Show("Incorrect Key");
}

TA贡献1873条经验 获得超9个赞
请参考https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/if-else
您没有将逻辑放入代码块中。基本上你的 if 语句什么都不做,代码继续处理每一行。
if (textBox1.Text == "Aim4last")
{
Main temp = new Main();
temp.Region = this.Region;
temp.Show();
this.Hide();
}
else if (textBox1.Text == "")
{
MessageBox.Show("Incorrect Key");
}
不能处理的是既不是空白的钥匙,也不是你想要的钥匙。您可能只想做一个 else:
if (textBox1.Text == "Aim4last")
{
Main temp = new Main();
temp.Region = this.Region;
temp.Show();
this.Hide();
}
else
{
MessageBox.Show("Incorrect Key");
}
- 2 回答
- 0 关注
- 107 浏览
添加回答
举报