0

On one Form, the Quiz form, I have an integer score which keeps track of the user's score. On another Form, the Game Over form, I need to print out the score of the user, but even after hours of me attempting to do so, I still can't manage to do it. I know my code could be improved by A LOT but I just need help to solve this problem and would appreciate it. I just started out on C# and I just wanted to experiment on things.

namespace Quiz_Application
{
    public partial class Quiz : Form
    {
        static Random gen = new Random();
        int[] rand = Enumerable.Range(1, 5).OrderBy(q => gen.Next()).ToArray();
        int i = 0;
        int score = 0;

    int[] goArray = new int[1];

    public void Question1()
    {
        questionText.Text = "What is the capital city of Spain?";
        optionA.Text = "Barcelona";
        optionB.Text = "Madrid";
        optionC.Text = "Seville";
        optionD.Text = "Zarazoga";
    }

    public void Question2()
    {
        questionText.Text = "What is the biggest island on Earth?";
        optionA.Text = "Luzon";
        optionB.Text = "Singapore";
        optionC.Text = "Greenland";
        optionD.Text = "Hawaii";
    }

    public void Question3()
    {
        questionText.Text = "What is the world's longest river?";
        optionA.Text = "Nile";
        optionB.Text = "Amazon";
        optionC.Text = "Mississipi";
        optionD.Text = "Congo";
    }

    public void Question4()
    {
        questionText.Text = "Which country is Prague in?";
        optionA.Text = "Czech Republic";
        optionB.Text = "Slovakia";
        optionC.Text = "Austria";
        optionD.Text = "Poland";
    }

    public void Question5()
    {
        questionText.Text = "What is the diameter of Earth?";
        optionA.Text = "6,779km";
        optionB.Text = "3,474km";
        optionC.Text = "12,742km";
        optionD.Text = "8,721km";
    }

    static void Wait(double sec)
    {
        Task.Delay(TimeSpan.FromSeconds(sec)).Wait();
    }

    public Quiz()
    {
        InitializeComponent();
    }

    private void Quiz_Load(object sender, EventArgs e)
    {

    }

    public void label1_Click(object sender, EventArgs e)
    {
        scoreNum.ResetText();
        score = 0;
        int goScore = goArray[0];

        this.Hide();
        Form1 f1 = new Form1();
        f1.ShowDialog();
        this.Close();
    }

    private void optionClick(object sender, EventArgs e)
    {
        startButton.Enabled = false;
        Button l = (Button)sender;
        int[] goArray = new int[1];
        goArray[0] = score;

        if (l.Text == "Madrid" || l.Text == "Greenland" || l.Text == "Amazon" || l.Text == "Czech Republic" || l.Text == "12,742km")
        { 
            score++;
            scoreNum.Text = score.ToString();
            correctOrWrong.Image = Resources.correct; Wait(1); correctOrWrong.Image = null;
        }
        else
        {
            correctOrWrong.Image = Resources.wrong; Wait(1); correctOrWrong.Image = null;
        }

        l.BackColor = System.Drawing.Color.Maroon;
        optionA.Enabled = false; optionB.Enabled = false; optionC.Enabled = false; optionD.Enabled = false;
        startButton.Enabled = true;
    }

    private void startButton_Click(object sender, EventArgs e)
    {
        Button b = (Button)sender;
        optionA.Enabled = true; optionB.Enabled = true; optionC.Enabled = true; optionD.Enabled = true;

        if (i == 5)
        {
            scoreNum.ResetText();
            score = 0;
            int goScore = goArray[0]; 

            b.Text = "Finish";
            this.Hide();
            Game_Over go = new Game_Over();
            go.ShowDialog();
            this.Close();
        }

        try
        {
            switch (rand[i])
            {
                case 1:
                    Question1();
                    i++;
                    break;
                case 2:
                    Question2();
                    i++;
                    break;
                case 3:
                    Question3();
                    i++;
                    break;
                case 4:
                    Question4();
                    i++;
                    break;
                case 5:
                    Question5();
                    i++;
                    break;
                case 6:
                    Wait(2);
                    this.Hide();
                    Game_Over go = new Game_Over();
                    go.ShowDialog();
                    this.Close();
                    break;
            }

            if (i == 5)
            {
                b.Text = "Finish";
            }

        }
        catch { }
        if (i != 5)
        b.Text = "Next";

        b.Enabled = false;
    }

    private void mouseEnter(object sender, EventArgs e)
    {
        this.Cursor = Cursors.Hand;
    }

    private void mouseLeave(object sender, EventArgs e)
    {
        this.Cursor = Cursors.Default;
    }

    public int get(int i)
    {
        return i;
    }

}

and my Game Over form

namespace Quiz_Application
{
    public partial class Game_Over : Form
    {

    public Game_Over()
    {
        InitializeComponent();
    }

    private void Game_Over_Load(object sender, EventArgs e)
    {
        Quiz q = new Quiz();
    }

    private void playAgain_Click(object sender, EventArgs e)
    {
        Quiz q = new Quiz();
        this.Hide();
        q.ShowDialog();
        this.Close();
    }

    private void mainMenu_Click(object sender, EventArgs e)
    {
        Form1 f1 = new Form1();
        this.Hide();
        f1.ShowDialog();
        this.Close();
    }
}
  • See my two form project at following posting : http://stackoverflow.com/questions/34975508/reach-control-from-another-page-asp-net – jdweng Nov 27 '16 at 14:35

2 Answers2

1

Create a public class and store the value in it, you can access the value across the code.

class Globals
    {
        public static int Score = 0; 
    }

// In the window you can assign value for the variable like below

Globals.Score=<Your score>;
Developer
  • 245
  • 1
  • 15
0

In the Game_Over form, add a label to display the score if you haven't done so already. I will call it myScoreLabel Then, when you show the form i.e. here:

case 6:
    Wait(2);
    this.Hide();
    Game_Over go = new Game_Over();
    go.ShowDialog();
    this.Close();
    break;

add this line before go.ShowDialog:

go.yourScoreLabel.Text = "Score: " + score.ToString();
Sweeper
  • 213,210
  • 22
  • 193
  • 313