【问题标题】:How to print my score on another Winform? [duplicate]如何在另一个 Winform 上打印我的分数? [复制]
【发布时间】:2016-11-27 14:25:22
【问题描述】:

在一个表格,即测验表格上,我有一个整数分数,用于跟踪用户的分数。在另一个表单上,即 Game Over 表单上,我需要打印出用户的分数,但即使经过数小时的尝试,我仍然无法做到。我知道我的代码可以改进很多,但我只需要帮助来解决这个问题,我会很感激的。我刚开始使用 C#,我只是想尝试一些东西。

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

}

还有我的 Game Over 表单

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

【问题讨论】:

标签: c# winforms


【解决方案1】:

创建一个公共类并将值存储在其中,您可以跨代码访问该值。

class Globals
    {
        public static int Score = 0; 
    }

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

Globals.Score=<Your score>;

【讨论】:

  • 不应将整数存储为字符串。
  • 好的,一定会记住的
【解决方案2】:

如果您还没有这样做,请在Game_Over 表单中添加一个标签以显示分数。我将其称为myScoreLabel 然后,当您显示表单时,即在这里:

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

go.ShowDialog之前添加这一行:

go.yourScoreLabel.Text = "Score: " + score.ToString();

【讨论】:

  • 这不起作用,因为标签不是公开的
  • @TalhaTalipAçıkgöz 好吧,你总是可以通过创建一个 getter 来公开它,或者只是标记它public
  • 可以通过文本框的修饰符属性来实现
  • 我认为根据我的评论编辑您的答案似乎很好。
  • 正如您已经说过的,您需要将标签设为公开,否则,谢谢!
猜你喜欢
  • 1970-01-01
  • 2021-07-29
  • 1970-01-01
  • 1970-01-01
  • 2018-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-16
相关资源
最近更新 更多