0

I am creating a test creator / taker in windows forms. I have an array that stores the questions (retreived from a text file), and I have a foreach loop that sets up each question. How can I, within that foreach loop, wait for the "next question" button to be pressed before I continue to the next iteration?

Here is the code:

foreach(string info in testInfo) // Start the test
        {
            int correctAwnser = Convert.ToInt32(info.Split('>')[5]); 
            //get the correct awnser from array and store it as an int

            SetUpQuestion(info); // Set up the question 
            RefreshAll();        // Refreshes everything on the form

            WaitNextPressed();   // Wait for "next" to be pressed

            AddQuestionToScore(correctAwnser); 
            // Add the question and correct awnser to the grade
        }

I need to find a way to create the method "WaitNextPressed()", that waits for the "next" button to be pressed.

Thanks in advance to anybody who helps me out

  • You can use TaskCompletionSource that is awaited and is triggered by the button click event handler. It does however require the subject member to use async/await. – Nkosi Feb 07 '21 at 01:40
  • I'm farly new to c#, are there any good tutorials on youtube I can watch to get familiar with these things? – Dat 1 channels Feb 07 '21 at 01:46
  • Instead of a foreach just keep the current index. and increment it when the user presses the button – TheGeneral Feb 07 '21 at 02:25
  • After thinking about it more, I have to. When a button is pressed in winforms all the code must be executed before you can press another button, so I have to redesign my whole program. yay – Dat 1 channels Feb 07 '21 at 03:49
  • 1
    Each iteration of the loop can create a `TaskCompletionSource`, the value of which is then set by your button's `Click` event handler. This allows you to retain the basic loop structure without blocking the UI thread, i.e. allowing user input to be handled correctly. See duplicate. – Peter Duniho Feb 07 '21 at 04:18

0 Answers0