如何在按钮按下时使用不同的列表?

huangapple go评论73阅读模式
英文:

How to use a different list on button press?

问题

我目前正在制作一个关于拼写的小测验。
程序显示一个问题和4个可能的答案。
现在我卡在的地方是我不知道如何更改正在使用的列表。
我有两个问题列表:StamQuestions和StateQuestions。

public void GiveAnswerA(object sender, EventArgs e)
{
    if (TextAnswerA.Text == StamQuestions[numberQuestion].CorrectAnswer)
    {
        SetUI();
        score++;
    }
}

private void GiveAnswerB(object sender, EventArgs e)
{
    if (TextAnswerB.Text == StamQuestions[numberQuestion].CorrectAnswer)
    {
        SetUI();
        score++;
    }
}

private void GiveAnswerC(object sender, EventArgs e)
{
    if (TextAnswerC.Text == StamQuestions[numberQuestion].CorrectAnswer)
    {
        SetUI();
        score++;
    }
}

private void GiveAnswerD(object sender, EventArgs e)
{
    if (TextAnswerD.Text == StamQuestions[numberQuestion].CorrectAnswer)
    {
        SetUI();
        score++;
    }
}

private void SetUI()
{
    numberQuestion = rnd.Next(0, StamQuestions.Count);

    CurrentQuestion.Text = StamQuestions[numberQuestion].QuestionText;
    TextAnswerA.Text = StamQuestions[numberQuestion].Answers[0];
    TextAnswerB.Text = StamQuestions[numberQuestion].Answers[1];
    TextAnswerC.Text = StamQuestions[numberQuestion].Answers[2];
    TextAnswerD.Text = StamQuestions[numberQuestion].Answers[3];

    ResultAnswer.Text = "Punten : " + score.ToString();
}

现在我想要的是,当我按下一个按钮时,StamQuestions被StateQuestions替换。

任何帮助将不胜感激!

编辑:这是列表的定义

List<Question> StamQuestions = new List<Question>();
List<Question> StateQuestion = new List<Question>();
英文:

I'm currently making a little Quiz about spelling.
The program shows a Question and 4 possible answers.
Now what I'm stuck on is that I don't know how to change the list that's being used.
I have 2 Lists of Questions: StamQuestions and StateQuestions.

 public void GiveAnswerA(object sender, EventArgs e)
 {
     if (TextAnswerA.Text == StamQuestions[numberQuestion].CorrectAnswer)
     {
         SetUI();
         score++;
     }
 }

 private void GiveAnswerB(object sender, EventArgs e)
 {
     if (TextAnswerB.Text == StamQuestions[numberQuestion].CorrectAnswer)
     {
         SetUI();
         score++;
     }
 }

 private void GiveAnswerC(object sender, EventArgs e)
 {
     if (TextAnswerC.Text == StamQuestions[numberQuestion].CorrectAnswer)
     {
         SetUI();
         score++;
     }
 }

 private void GiveAnswerD(object sender, EventArgs e)
 {
     if (TextAnswerD.Text == StamQuestions[numberQuestion].CorrectAnswer)
     {
         SetUI();
         score++;
     }
 }

 private void SetUI()
 {
     numberQuestion = rnd.Next(0, StamQuestions.Count);

     CurrentQuestion.Text = StamQuestions[numberQuestion].QuestionText;
     TextAnswerA.Text = StamQuestions[numberQuestion].Answers[0];
     TextAnswerB.Text = StamQuestions[numberQuestion].Answers[1];
     TextAnswerC.Text = StamQuestions[numberQuestion].Answers[2];
     TextAnswerD.Text = StamQuestions[numberQuestion].Answers[3];

     ResultAnswer.Text = &quot;Punten : &quot;+score.ToString();
 }

Now what I want to happen is that when I press a button, the StamQuestions get replaced by StateQuestions.

Any help would be appreciated!

Edit : Here is definition of the lists

        List&lt;Question&gt; StamQuestions = new List&lt;Question&gt;();
        List&lt;Question&gt; StateQuestion = new List&lt;Question&gt;();

答案1

得分: 4

通常情况下,您只需存储对当前使用的问题列表的引用,并根据需要切换该引用。

添加另一个类级变量currentQuestions

private List<Question> stamQuestions = new List<Question>();
private List<Question> stateQuestions = new List<Question>();
private List<Question> currentQuestions = stamQuestions;

在所有地方使用currentQuestions,例如:

public void GiveAnswerA(object sender, EventArgs e)
{
    if (TextAnswerA.Text == currentQuestions[numberQuestion].CorrectAnswer)
    {
        SetUI();
        score++;
    }
}

然后根据需要切换到另一个集合,我猜测在SetUI方法中:

private void SetUI()
{
    currentQuestions = stateQuestions;

    numberQuestion = rnd.Next(0, currentQuestions.Count);
    CurrentQuestion.Text = currentQuestions[numberQuestion].QuestionText;
    TextAnswerA.Text = currentQuestions[numberQuestion].Answers[0];
    TextAnswerB.Text = currentQuestions[numberQuestion].Answers[1];
    TextAnswerC.Text = currentQuestions[numberQuestion].Answers[2];
    TextAnswerD.Text = currentQuestions[numberQuestion].Answers[3];
    ResultAnswer.Text = "Punten : " + score.ToString();
}

或者也许您打算在列表之间进行切换,如果是这样,您还可以这样做:

currentQuestions = (currentQuestions == stamQuestions) ? stateQuestions : stamQuestions;
英文:

In general you would just store a reference to the currently used list of questions, and swap that reference as needed.

Add another class-level variable for currentQuestions:

private List&lt;Question&gt; stamQuestions = new List&lt;Question&gt;(); 
private List&lt;Question&gt; stateQuestions = new List&lt;Question&gt;();
private List&lt;Question&gt; currentQuestions = stamQuestions;

Use currentQuestions everywhere, for example:

public void GiveAnswerA(object sender, EventArgs e)
{
    if (TextAnswerA.Text == currentQuestions[numberQuestion].CorrectAnswer)
    {
        SetUI();
        score++;
    }
}

Then swap to the other set as needed, I suspect in SetUI:

private void SetUI()
{
    currentQuestions = stateQuestions;

    numberQuestion = rnd.Next(0, currentQuestions.Count);
    CurrentQuestion.Text = currentQuestions[numberQuestion].QuestionText;
    TextAnswerA.Text = currentQuestions[numberQuestion].Answers[0];
    TextAnswerB.Text = currentQuestions[numberQuestion].Answers[1];
    TextAnswerC.Text = currentQuestions[numberQuestion].Answers[2];
    TextAnswerD.Text = currentQuestions[numberQuestion].Answers[3];
    ResultAnswer.Text = &quot;Punten : &quot;+score.ToString();
}

Or maybe you intended to flip/flop between lists in which case you could also do:

currentQuestions = (currentQuestions == stamQuestions) ? stateQuestions : stamQuestions;

答案2

得分: 2

创建一个新的私有属性:

private List<Question> Questions { get; set; }

然后始终使用此属性:

TextAnswerD.Text == Questions[numberQuestion].CorrectAnswer;
...

并将其更改为:

Questions = StamQuestions;

Questions = StateQuestion;
英文:

Create a new private property:

private List&lt;Question&gt; Questions { get; set; }

then always use this property:

TextAnswerD.Text == Questions[numberQuestion].CorrectAnswer; 
...

And change it like this:

Questions = StamQuestions;

or

Questions = StateQuestion;

huangapple
  • 本文由 发表于 2020年1月6日 18:17:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610275.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定