为了账号安全,请及时绑定邮箱和手机立即绑定

在白名单密钥系统上需要帮助

在白名单密钥系统上需要帮助

C#
隔江千里 2022-07-10 16:10:35
我试图让人们需要得到一个密钥,然后他们必须将密钥输入一个文本框,然后单击一个完成按钮,如果它是正确的,我希望它把它们带到不同的形式。问题是我希望它是如果键是其他任何东西而不是正确的键它会显示一个 mbox 说不正确的键并保持在同一个表单上,但是每次我尝试做类似的事情时它只会带我到下一个表格,仍然说不正确的密钥。private void button1_Click_1(object sender, EventArgs e){    if (textBox1.Text == "Aim4last") ;    Main temp = new Main();    temp.Region = this.Region;    temp.Show();    this.Hide();    if (textBox1.Text == "") ;    MessageBox.Show("Incorrect Key");}
查看完整描述

2 回答

?
慕运维8079593

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");

}


查看完整回答
反对 回复 2022-07-10
?
眼眸繁星

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");

}


查看完整回答
反对 回复 2022-07-10
  • 2 回答
  • 0 关注
  • 107 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号