Is an object reference required for a non-static field, method or property?

I know this is probably a very newbie, so I apologize.

I am trying to access the Text property of a label on Form1 from another MaxScore form.

When I click the Ok button on MaxScore, I want to set Form1 myGameCountLbl.Text to the Form1 max variable using max.ToString ().

Here is my code in the OK MaxScore button event:

private void okBtn_Click(object sender, EventArgs e)
{
    Form1.myGameCountLbl.Text = Form1.max.ToString();
    Form1.compGameCountLbl.Text = Form1.max.ToString();
}

But when I am going to compile it, I get an error message:

An object reference is required for a 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 these two labels are also pubic.

Thanks!

( lassevk !):

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

Form1 ?

.

okBtn , MaxScore Form1.

, MaxScore:

public Form1 MainForm { get; set; }

okBtn_Click :

private void okBtn_Click(object sender, EventArgs e)
{
    MainForm.myGameCountLbl.Text = MainForm.max.ToString();
    MainForm.compGameCountLbl.Text = MainForm.max.ToString();
}

, MaxScore Form1 ( , ):

using (MaxScore scoreForm = new MaxScore())
{
    scoreForm.MainForm = this;
    scoreForm.ShowDialog();
}
+7

@lassevk . Form1, / , , . Form1, . MaxScore , .

.

+2

All Articles