0

So, my XML file is as follows:

<?xml version="1.0" encoding="utf-8" ?>
<quiz>

</answers>
</question>

<!-- More questions here  -->

</quiz>

My Form1.cs looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Drawing;

namespace WindowsFormsApplication1
{
    public partial class FormMain : Form
    {

Basically, all four buttons had the question on them rather than the answers and underneath the bottom left button, there appeared to be a blank button. How do I get the question to display where label1 is and the answers to be on four separate buttons? Also, how will I do it when I start adding multiple questions (User obviously can't move on to the next question unless they get previous one right and difficulty can be chosen at the start to show a different set of questions).

I've been on at this for a while and I think it needs a fresh set of eyes because my relatively novice C# brain can't figure it out. Anyone help me please?

2 Answers2

0

Check out the answer to this question. What is much easier to deal with is Deserialize the XML into a class you've created with a matching structure.

How to Deserialize XML document

More info here: http://msdn.microsoft.com/en-us/library/58a18dwa.aspx

Community
  • 1
  • 1
corylulu
  • 3,449
  • 1
  • 19
  • 35
0

You are setting the text of every button each time around your loop:

foreach (var question in _questions)
{
    button1.Text = question.QuestionText;
    button2.Text = question.QuestionText;
    button3.Text = question.QuestionText;
    button4.Text = question.QuestionText;
}

The last time around the loop, each button will have the text set to the text of the last question.

It also looks a bit odd, as you have four buttons defined as fields of the form, yet you are creating additional buttons in your PopulateForm method but doing nothing with them.

You would be better off getting rid of the fields and working with the newly-created buttons directly:

private void PopulateForm()
{
    int count = 0;

    foreach (var question in _questions)
    {
        var button = new Button();
        button.Size = new Size(60, 23);
        button.Location = new Point(100, 40 + (count * 30));
        button.Text = question.QuestionText;
        Controls.Add(button);
        count++;
    }
}

You have to set the location of each button to something different, otherwise they will all show in the same place.


EDIT:

From looking at your code in your zip file, what you want is something like (assuming that your questions have four answers):

private void PopulateForm()
{
    foreach (var question in _questions)
    {
        label1.Text = question.QuestionText;

        button1.Text = question.Answers[0];
        button2.Text = question.Answers[1];
        button3.Text = question.Answers[2];
        button4.Text = question.Answers[3];
    }
}
adrianbanks
  • 81,306
  • 22
  • 176
  • 206
  • So get rid of the Button[] myBut = new Button[3]; for (int i = 0; i < 3; i++) stuff and replace it with the above? How do I set the location of each individual button if it's looping around? – user2084095 Feb 25 '13 at 23:39
  • @user2084095: Yes. To set the position of each button, you could just add a set amount each time to its position. – adrianbanks Feb 25 '13 at 23:41
  • I tried doing that but each button is just blank. I've never done stuff like this before; how do I add a set amount to its position? It's doing this: http://i.imgur.com/V8Ejqst.png - I want label1 to display the question from the XML file and the four blank buttons to have the four possible answers on it, without the annoying overlapping button with text in it. – user2084095 Feb 25 '13 at 23:44
  • Oh my God, thank you! One last question, how would I do this with say 10 questions and/or questions with different difficulty () with 1 being easy, 2 medium and 3 hard. Seriously though man, thanks a ton, I can now see where I was going wrong; I was overthinking it – user2084095 Feb 26 '13 at 00:05
  • @user2084095: You could just add more buttons to deal with more answers (and hide the extra buttons if not all questions have the same number of answers using their [`Visible`](http://msdn.microsoft.com/en-gb/library/system.windows.forms.control.visible.aspx) property). Not too sure what you mean by the difficulties. – adrianbanks Feb 26 '13 at 00:09
  • All questions have four answers so how would I do that then? By difficulties, I mean one set of questions has difficulty="1" so how would I create a button saying like 'Easy' so that it only shows the questions with difficulty="1" and the same for 'Medium' and 'Hard' (difficulty="2" and 3 in these cases) – user2084095 Feb 26 '13 at 00:16
  • I would suggest that filter your questions into "buckets" based on their difficulty, then switch the bucket that you use to update the UI depending on the selected difficulty. – adrianbanks Feb 26 '13 at 00:18
  • OK, are there any useful pages/documentation on how I would do this? – user2084095 Feb 26 '13 at 00:23
  • Learn about [`Dictionary`](http://msdn.microsoft.com/en-gb/library/xfhwa508.aspx) and [`List`](http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx) - they will both be of help in doing this. If you are still having another specific issue, try posing another question on StackOverflow. – adrianbanks Feb 26 '13 at 00:25
  • Hmm, I shall take a look. Is there a way to randomise the order of the questions and stop it from repeating the same question more than once per quiz – user2084095 Feb 26 '13 at 09:06
  • Try generating a random number and getting the item at that position in the list of questions. – adrianbanks Feb 26 '13 at 12:51