Hi there, I know this is probably a very newbish question, so I apologize.
I am trying to access the Text property of a label on Form1 from another form, MaxScore.
When I click the Ok button on MaxScore, I want to set Form1's myGameCountLbl.Text to Form1's variable, max by using max.ToString().
Here is my code in the OK button event of MaxScore:
private void okBtn_Click(object sender, EventArgs e)
{
Form1.myGameCountLbl.Text = Form1.max.ToString();
Form1.compGameCountLbl.Text = Form1.max.ToString();
}
But when I go to compile it, I get the error:
An object reference is required for the non-static field, method, or property 'Towergame_2.Form1.myGameCountLbl'
I get the same error for Towergame_2.Form1.max and Towergame_2.Form1.compGameCountLbl.
Not quite sure how to fix this. Max is a public variable and the two labels are pubic as well.
Thanks!
This is the code in my constructor (thank you lassevk for the code!):
public Form1()
{
//initialize vars
myHp = 100;
compHp = 100;
youWon = 0;
compWon = 0;
money = 100;
canCompAttack = true;
gameOver = false;
//show HowToPlay Dialogue
HowToPlay howToPlay = new HowToPlay();
howToPlay.ShowDialog();
using (MaxScore maxScore = new MaxScore())
{
maxScore.MainForm = this;
maxScore.ShowDialog();
}
InitializeComponent();
}