首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >验证与数字自动跳变

验证与数字自动跳变
EN

Stack Overflow用户
提问于 2015-04-30 02:20:18
回答 1查看 56关注 0票数 0

为了验证使用,我做了一个简单的界面。第一,您在"txtinput“中插入文本,如果您插入除单词"hello”以外的内容,它将弹出消息框“请再次尝试”.Then,在"txtchance“处,数字"3”将自动更改为"2".Again无效输入,它将有机会"1“和"0”然后消息框显示“不再尝试”并退出应用程序。

我面临的问题是,当我使用"for循环“自动更改"txtchance”号时,它会循环所有不停止的数字,让我再试一次,退出application.How来停止它还是破坏它?

这里是代码

代码语言:javascript
运行
复制
 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();
        }
    }
}

这里的电影短片 https://www.imageupload.co.uk/image/Zl1K

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-30 02:35:57

根本不需要for循环。您真正想做的只是检查您的tries字段:

代码语言:javascript
运行
复制
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();
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29957942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档