为了验证使用,我做了一个简单的界面。第一,您在"txtinput“中插入文本,如果您插入除单词"hello”以外的内容,它将弹出消息框“请再次尝试”.Then,在"txtchance“处,数字"3”将自动更改为"2".Again无效输入,它将有机会"1“和"0”然后消息框显示“不再尝试”并退出应用程序。
我面临的问题是,当我使用"for循环“自动更改"txtchance”号时,它会循环所有不停止的数字,让我再试一次,退出application.How来停止它还是破坏它?
这里是代码
public partial class Form1 : Form
{
public int tries = 2;
public Form1()
{
InitializeComponent();
txtchance.Text = "3";
}
private void btn_click_Click(object sender, EventArgs e)
{
if (txtinput.Text != string.Empty)
{
if (txtinput.Text == "hello")
{
textBox1_Validated(sender, e);
}
else
{
textBox1_Validating(sender, e);
}
}
}
private void textBox1_Validating(object sender, EventArgs e)
{
if (tries > 0)
{
for (int i = 3; i >= 0; i--)
{
txtchance.Text = i.ToString();
if (i > 0)
{
tries--;
txtinput.Clear();
MessageBox.Show("Please try again", "Error");
}
else
{
MessageBox.Show("Sorry, no more tries", "Error");
Application.Exit();
}
}
}
}
private void textBox1_Validated(object sender, EventArgs e)
{
if (tries != 0)
{
MessageBox.Show("Well done, you managed to enter a valid input!", "Validation OK");
this.Close();
}
}
}
发布于 2015-04-30 02:35:57
根本不需要for
循环。您真正想做的只是检查您的tries
字段:
private void textBox1_Validating(object sender, EventArgs e)
{
txtchance.Text = tries.ToString();
if (tries-- > 0)
{
txtinput.Clear();
MessageBox.Show("Please try again", "Error");
}
else
{
MessageBox.Show("Sorry, no more tries", "Error");
Application.Exit();
}
}
https://stackoverflow.com/questions/29957942
复制相似问题