2016-12-13 15 views
0

"GameForm"を呼び出す "form1"があります。それは次のようなものです:フォームに別のフォームが閉じる方法を教えてください

  var tmp = new Pjeu(P1, P2); 
      tmp.Show(); 

"P"はクラスです。 「GameForm」で、それは最終的に勝者を見つけて、そのようなのような自分自身を閉じます。

   MessageBox.Show("Victory " + label3.Text, "Game Finished", MessageBoxButtons.OK); 
       this.Close(); 

どのように私は、「Form1は」それが閉じられたことを知ることができますか?私は2回勝者を見つけるためにゲームを2回行う必要があるので、私はゲームを2回行うために私の最初のフォームが必要で、もう1つを作るためにそれが閉じられるのを待つ。私はいくつかの答えを見つけましたが、私の文脈ではうまくいきません

答えて

0

GameFormでは、カスタムイベントを作成して、フォームを閉じる前にそのイベントを呼び出すことができます。 Form1では、このイベントに登録するだけです。 GameFormで

:あなたのForm1で

public delegate void Winner(); 
public event Winner OnWinnerFound; 
... 
OnWinnerFound(); // Just call your event 
MessageBox.Show("Victory " + label3.Text, "Game Finished", MessageBoxButtons.OK); 
      this.Close(); 

var tmp = new Pjeu(P1, P2); 
tmp.OnWinnerFound+=WinnerFound; 
tmp.Show(); 
... 
private void WinnerFound() 
{ 
    //Your code here 
} 

私は多分、フォームが閉じたりしたときに発生するイベントであっ内蔵されて、覚えていませんフォームは終了しています。

0

GameFormを作成するときに、FormClosingイベントハンドラを追加します。

GameForm MyForm = new GameForm(); 
MyForm.FormClosing += new FormClosingEventHandler(GameForm_Closing); 
MyForm.Show(); 

フォームがcompletley閉じる前に、あなたがその値にアクセスできるように続いてハンドラを作成するには、Get-設定

public string Winner {get; set;} //just an example 

を使用して優勝者を意味しGameForm内の変数にアクセスできることを確認します。

private void GameForm_Closing(object sender, FormClosingEventArgs e) 
{ 
    var obj = (GameForm)sender; 
    myForm1WinnerVariable = obj.Winner; 
    //or any other code you want to do as the form closes (eg, increment a variable) 
} 
関連する問題