本文介绍了延迟按钮不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试整理一个用于培训的小项目.用户必须在有限的时间内输入问题的答案.时间到期后,用户将不会获得答案的信用,而是显示正确的答案.当然,当用户按下下一步"按钮时,直到超时到期,才应显示正确的答案.在此期间,用户必须输入答案.我试图在下一步"按钮中放置延迟.

显示问题的简化代码:

I''m trying to put together a small project used for training. A user must enter an answer to the problem, given a limited time. When time is expired, the user is not credited for the answer, correct answer shown. Of course, when the user presses Next button, the correct answer should not be shown until timeout is expired. During this time the user nust enter the answer. I tried to put delay in the Next button.

Simplified code to show the problem:

using System;
using System.Threading;
using System.Windows.Forms;
namespace DelayButton
    {
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Form MyForm = this;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            Thread.Sleep(10000);
            this.textBox1.Text = "Some answer...";
            button1.Enabled = true;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Can use second button!");
            CheckupAnswer(listBox1.SelectedIndex);
        }
        void CheckupAnswer(int answerIndex) { }
    }
}



它可以工作,但是在此期间,用户无法选择答案.此外,显示了沙漏光标,但显示的时间要晚得多,而且并非总是如此.我不明白为什么.在此期间,我不需要下一步"按钮,因此我将其禁用.其他控件如何不响应?

有人可以帮我解决延迟问题吗?



It works, but during this time the user cannot select the answer. Also, hourglass cursor is shown, but much later and not always. I don''t understand why. I don''t need Next button during this time, so I disable it. How come other controls do not respond?

Could anyone help me to solve delay problem?

推荐答案


System.Windows.Forms.Timer tmr = new System.Windows.Forms.Timer();
void tmr_Tick(object sender, EventArgs e)
{
  tmr.Stop();
  this.textBox1.Text = "Some answer...";
  button1.Enabled = true;
}

private void button1_Click(object sender, EventArgs e)
{
  button1.Enabled = false;
  tmr.Interval = 10000;
  tmr.Tick += new EventHandler(tmr_Tick);
  tmr.Start();
}



这篇关于延迟按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 23:31
查看更多