简体   繁体   中英

Triggering button clicks

I am brand new to C#. I actually studied Java years ago at University but obviously didn't learn it very well! Now I'm very keen on learning C# from the ground up. I have a decent idea of the absolute basics, but have now come unstuck.

I am building a relatively simple Quiz "Game" application using WPF. The user clicks the "Load Quiz" button, which loads a text file which contains questions and answers. The user then clicks "Next Question", which loads the first question and the four multiple choices into text boxes. This all seems to be working ok.

The user then clicks on the button 1/2/3/4 corresponding to the correct answer. When they do this, I want the "Correct! Well done" text to display in tResult if the correct answer is chosen, or the other text if answer is incorrect.

However, clicking a button makes no immediate action, until bNextQ_Click is clicked, when finally the "Sorry, that is incorrect" displays in tResult.

It would appear that the boolean flags are remaining false, so there is no recognition of the click event.

PLEASE NOTE: I realise my code will be a mess and I am sure that I am doing this in a bad and inefficient manner. Any advice gratefully accepted!

Anyway, here is the code, any feedback is much appreciated:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace QuizGame
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();
    }

    private bool button1Clicked = false;
    private bool button2Clicked = false;
    private bool button3Clicked = false;
    private bool button4Clicked = false;

    private int score = 0;

    private void txtQuestion_TextChanged()
    {
        throw new NotImplementedException();
    }

    private void bNextQ_Click(object sender, RoutedEventArgs e)
    {
        questionIteration();

    }

    private void numQuestions_TextChanged(object sender, TextChangedEventArgs e)
    {

    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        button1Clicked = true;
    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        button2Clicked = true;
    }

    private void button3_Click(object sender, RoutedEventArgs e)
    {
        button3Clicked = true;
    }

    private void button4_Click(object sender, RoutedEventArgs e)
    {
        button4Clicked = true;
    }

    private void txtQuestion_TextChanged(object sender, TextChangedEventArgs e)

    {
        //txtQuestion.Text = splitted[];

    }

    private void button_Load_Quiz_Click(object sender, RoutedEventArgs e)
    {
        int lineCount = File.ReadLines(@"C:\\Users\Morgan\Documents\Visual Studio 2010\Projects\QuizGame\QuizGame\Quiz.txt").Count();

        numQuestions.Text = lineCount.ToString();

        txtQuestion.Text = "Press 'Next Question' to start.";
    }

    private void questionIteration()
    {


        int i = 0;

        string[] questionArray = File.ReadAllLines(@"C:\\Users\Morgan\Documents\Visual Studio 2010\Projects\QuizGame\QuizGame\Quiz.txt");

        foreach (string qArrays in questionArray)
        {


            string question = questionArray[i];

            string[] splitted = question.Split('|');

            txtQuestion.Text = splitted[0];
            textBox1.Text = splitted[1];
            textBox2.Text = splitted[2];
            textBox3.Text = splitted[3];
            textBox4.Text = splitted[4];

            string correct = splitted[5];

            if (button1Clicked == true)
            {
                if (correct == textBox1.Text)
                {
                    score++;

                    tResult.Text = "Correct! Well done.";
                }
                else tResult.Text = "Sorry, that is incorrect.";

            }

            if (button2Clicked == true)
            {
                if (correct == textBox2.Text)
                {
                    score++;

                    tResult.Text = "Correct! Well done.";
                }
                else tResult.Text = "Sorry, that is incorrect.";

            }


            if (button3Clicked == true)
            {
                if (correct == textBox3.Text)
                {
                    score++;

                    tResult.Text = "Correct! Well done.";
                }
                else tResult.Text = "Sorry, that is incorrect.";

            }


            if (button4Clicked == true)
            {
                if (correct == textBox4.Text)
                {
                    score++;

                    tResult.Text = "Correct! Well done.";
                }
                else tResult.Text = "Sorry, that is incorrect.";

            }



            i++;
            button1Clicked = false;
            button2Clicked = false;
            button3Clicked = false;
            button4Clicked = false;
        }

    }

}
}

clicking a button makes no immediate action, until bNextQ_Click is clicked

Thats because you have coded the "action" part, not immediately, but inside the questionIteration() function which will obviously fire when the next question's button is clicked. To fix this, put this part of code in a separate function and call it each time an answer button is clicked:

if (button1Clicked == true)
        {
            if (correct == textBox1.Text)
            {
                score++;

                tResult.Text = "Correct! Well done.";
            }
            else tResult.Text = "Sorry, that is incorrect.";

        }

        if (button2Clicked == true)
        {
            if (correct == textBox2.Text)
            {
                score++;

                tResult.Text = "Correct! Well done.";
            }
            else tResult.Text = "Sorry, that is incorrect.";

        }


        if (button3Clicked == true)
        {
            if (correct == textBox3.Text)
            {
                score++;

                tResult.Text = "Correct! Well done.";
            }
            else tResult.Text = "Sorry, that is incorrect.";

        }


        if (button4Clicked == true)
        {
            if (correct == textBox4.Text)
            {
                score++;

                tResult.Text = "Correct! Well done.";
            }
            else tResult.Text = "Sorry, that is incorrect.";

        }

Call this function after you have set the bool flags buttonxClicked, etc.

However, clicking a button makes no immediate action, until bNextQ_Click is clicked, when finally the "Sorry, that is incorrect" displays in tResult.

That's because that code is written like that. There is no bug or something, it's design flaw. Your button (1-4) clicks, actually do nothing more than setting flags. That's why nothing is happening. Next button actually runs code that sets some messages. You have to think about solution more carefully, about what happens and in what order. Right now Your code does something like that:

1 User clicks load button - quiz is loaded

2 User clicks 1-4 - flag is set

3 User clicks next - question is loaded again(what for?), and foreach question(why, if user answered one quesion):

3A Answers are set (again - what for?)

3B Check if user got answer right

3C reset flags

3D check next question(with resseted flags...)

It should be more like this:

1 User click load buton - quiz is loaded, show a message that its ready

2 User click next - load next question

3 User click 1-4 - there is check if answer is right, message is shown

4 User click next...

Think about Your solution, write down some order, some "algorithm" on a sheet of paper and the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM