触发按钮点击

时间:2012-08-09 11:33:24

标签: c# wpf events button

我是C#的新手。我几年前在大学学习Java,但显然没有很好地学习它!现在我非常热衷于从头开始学习C#。我对绝对基础知识有一个不错的想法,但现在已经解开了。

我正在使用WPF构建一个相对简单的测验“游戏”应用程序。用户单击“加载测验”按钮,该按钮加载包含问题和答案的文本文件。然后,用户单击“下一个问题”,将第一个问题和四个多个选项加载到文本框中。这一切似乎都正常。

然后用户点击与正确答案对应的按钮1/2/3/4。当他们这样做时,如果选择了正确的答案,我希望在TResult中显示“正确!完成”文本,如果答案不正确,我希望显示其他文本。

但是,单击按钮不会立即执行操作,直到单击bNextQ_Click,最后在tResult中显示“抱歉,不正确”。

看起来布尔标志保持为false,因此无法识别click事件。

请注意:我意识到我的代码将是一团糟,我确信我是以一种糟糕而低效的方式做到这一点。任何建议都感激不尽!

无论如何,这是代码,非常感谢任何反馈:

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;
        }

    }

}
}

2 个答案:

答案 0 :(得分:1)

  

单击按钮不会立即执行操作,直到单击bNextQ_Click

多数民众赞成因为你已经编写了“动作”部分,而不是立即编码,而是在questionIteration()函数中编写,当单击下一个问题的按钮时,这个函数显然会触发。要解决此问题,请将此部分代码放在单独的函数中,并在每次单击答案按钮时调用它:

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.";

        }

设置bool标志按钮xClicked等后调用此函数

答案 1 :(得分:0)

  

但是,单击按钮不会立即执行操作,直到单击bNextQ_Click,最后在tResult中显示“抱歉,不正确”。

那是因为代码是这样编写的。没有错误或其他什么,这是设计缺陷。你的按钮(1-4)点击,实际上只做设置标志。这就是为什么没有发生的事情。下一步按钮实际上运行设置一些消息的代码。你必须更仔细地考虑解决方案,关于发生的事情和顺序。现在你的代码做了类似的事情:

1用户点击加载按钮 - 加载测验

2用户点击1-4 - 设置标志

3用户点击下一步 - 问题再次加载(用于什么?)和foreach问题(如果用户回答了一个问题,为什么):

3A答案已设定(再次 - 为什么?)

3B检查用户是否得到了正确答案

3C重置标志

3D检查下一个问题(带有重新标记的标志......)

应该更像这样:

1用户点击加载按钮 - 加载测验,显示准备就绪的消息

2用户点击下一步 - 加载下一个问题

3用户点击1-4 - 检查答案是否正确,是否显示消息

4用户点击下一步...

想想你的解决方案,在一张纸和代码上写下一些订单,一些“算法”。