我是C#的全新品牌。我多年前在大学学习Java,但显然没有很好地学习它!现在我非常热衷于从头开始学习C#。我对绝对的基本知识有一个体面的想法,但现在已经变得不那么敏感了。触发按钮单击
我正在使用WPF构建一个相对简单的测验“游戏”应用程序。用户点击“加载测验”按钮,加载包含问题和答案的文本文件。然后用户单击“下一个问题”,它将第一个问题和四个多个选项加载到文本框中。这一切似乎都工作正常。
然后用户点击对应于正确答案的按钮1/2/3/4。当他们这样做时,如果选择了正确答案,我希望“正确!完成”文本显示在tResult中,如果答案不正确,则显示其他文本。
但是,单击按钮不会立即采取行动,直到单击bNextQ_Click,最后在tResult中显示“Sorry,that is incorrect”。
看来布尔标志仍然是假的,所以没有识别点击事件。
请注意:我意识到我的代码会很混乱,我相信我正在以糟糕和低效率的方式执行此操作。任何建议感激地接受!
总之,这里是代码,任何反馈是大加赞赏:
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;
}
}
}
}
问题应该是具体的。你是否期待我们编写你的程序? – Prabhavith 2012-08-09 11:36:27
C#的全新品牌?哈哈哈哈。 – 2012-08-09 11:47:45
更严肃的说明。不要马上去找WPF。去控制台或WinForms应用程序。 – 2012-08-09 11:48:36