Click here to Skip to main content
15,896,372 members

A button with delay does not work

programmer095 asked:

Open original thread
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:

C#
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?
Tags: C#, Windows Forms

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900