2016-11-27 8 views
0

1つのフォーム、クイズフォームには、ユーザーのスコアを記録する整数スコアがあります。ゲームオーバフォームの別のフォームでは、ユーザーのスコアを印刷する必要がありますが、私が何時間もそれを試みても、それでもやり遂げることはできません。私のコードはA LOTによって改善されるかもしれませんが、私はこの問題を解決するために助けが必要であり、それを感謝するでしょう。私はちょうどC#を使い始めました。私は物事を実験したかったのです。別のWinformで自分のスコアを印刷するには?

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

} 

と私のゲーム

フォームオーバー
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(); 
    } 
} 
+0

、次の投稿で私の2つのフォームプロジェクトを参照してください:http://stackoverflow.comを/ questions/34975508/reach-control-from-another-page-asp-net – jdweng

答えて

0

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(); 
+0

ラベルが公開されていないので、これは動作しません。 –

+1

@TalhaTalipAçıkgözまあ、ゲッターを作成するか、単に「public」にするだけで、公開することができます。 – Sweeper

+1

あなたはテキストボックスの修飾子プロパティを使ってそれを作ることができます –

1

パブリッククラスを作成し、その中に値を格納し、コード全体で値にアクセスすることができます。

class Globals 
    { 
     public static int Score = 0; 
    } 

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

Globals.Score=<Your score>; 
+0

文字列として整数を格納しないでください。 –

+0

さて、必ず覚えておいてください – Developer

関連する問題