views:

1872

answers:

2

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

Is by any chance Form1 the name of the class?

You need to have a reference to an instance of the form class.

Since okBtn is not on the same form, you need to give the MaxScore form a reference to the Form1 instance.

For instance, you can add this to your MaxScore form:

public Form1 MainForm { get; set; }

And then in your okBtn_Click method, you'll write this:

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

and then when you're constructing MaxScore from Form1 (I'm assuming that's what you're doing):

using (MaxScore scoreForm = new MaxScore())
{
    scoreForm.MainForm = this;
    scoreForm.ShowDialog();
}
Lasse V. Karlsen
When I add all this and try to compile and run, press ok on the MaxScore button, I get an exception at: MainForm.myGameCountLbl.Text = MainForm.max.ToString();saying: NullReferenceException was unhandled. Object reference not set to an instance of an object.
Eric
Did you actually set MainForm, as I showed in the last piece of code there?
Lasse V. Karlsen
Yes I did. I have the last piece of code in my Form1 constructor.
Eric
After compiling, it gives me a warning about this code: MainForm.myGameCountLbl.Text = MainForm.max.ToString(); the warning is: Accessing a member on 'Towergame_2.Form1.max' may cause a runtime exception because it is a field of a marshal-by-reference classBut when I try to run it, then it gives me an exception when I press the OK button.
Eric
I think you need to post the code that constructs and opens the MaxScore form, the code I showed should work, but of course if there are other problems that impact this, it won't.
Lasse V. Karlsen
I added the code of my constructor to the post ^^
Eric
+1  A: 

I agree with @lassevk with regards to resolving your issue. I'd also recommend wrapping the behavior of setting the labels into a method within the Form1 class, which simply helps keep your code cleaner and keeps the responsibility/knowledge of what fields to update and how to update them contained within the parent form. You'd simply define a public method in Form1 that takes a string value and updates the specific labels with that value. Then in the MaxScore form, in your button click event handler, you'd call that method rather than try to access those label controls directly.

Just food for thought.

mannish
Thanks for the great idea!
Eric