'Passing values between windows forms
I have a GameSelection form and when a specific buttons is been hit, the GameSelection form will become hidden and the game form will start. When the game form has been closed, the GameSelection form will be shown again without creating a new one through FormClosedEventHandler. My question is: How would I make the game form pass a value to the GameSelection form, such as score for example?
Here is some of my code:
private void PanOptionsClick(object sender, EventArgs e)
{
Button Temp = sender as Button;
if(Temp != null)
{
if (PreTag == "FirstGame")
{
switch(Temp.Text)
{
case "Easy":
OddsAndEvensGame g1 = new OddsAndEvensGame(15, new Vector2(1, 30));
g1.Show();
g1.FormClosed += new FormClosedEventHandler(OddsAndEvens_Closed);
this.Hide();
break;
case "Hard":
OddsAndEvensGame g2 = new OddsAndEvensGame(20, new Vector2(1, 60));
g2.Show();
g2.FormClosed += new FormClosedEventHandler(OddsAndEvens_Closed);
this.Hide();
break;
case "Insane":
OddsAndEvensGame g3 = new OddsAndEvensGame(25, new Vector2(1, 120));
g3.Show();
g3.FormClosed += new FormClosedEventHandler(OddsAndEvens_Closed);
this.Hide();
break;
case "InHuman":
OddsAndEvensGame g4 = new OddsAndEvensGame(25, new Vector2(1, 1000));
g4.Show();
g4.FormClosed += new FormClosedEventHandler(OddsAndEvens_Closed);
this.Hide();
break;
}
}
}
Solution 1:[1]
You can add a class, by going project add class, and name it something like GlobalVariables. Write variables with 'public static'.
So with your example:
public static int Score;
From you other forms you can now access and change this variable by calling the global variables form and the score:
GlobalVariables.Score = 3;
using 3 as the example here
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
