非静态字段,方法或属性需要对象引用吗?

时间:2022-09-04 11:07:54

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.

我试图从另一种形式MaxScore访问Form1上的标签的Text属性。

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().

当我单击MaxScore上的Ok按钮时,我想通过使用max.ToString()将Form1的myGameCountLbl.Text设置为Form1的变量max。

Here is my code in the OK button event of MaxScore:

这是我在MaxScore的OK按钮事件中的代码:

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'

非静态字段,方法或属性“Towergame_2.Form1.myGameCountLbl”需要对象引用

I get the same error for Towergame_2.Form1.max and Towergame_2.Form1.compGameCountLbl.

我得到了Towergame_2.Form1.max和Towergame_2.Form1.compGameCountLbl的相同错误。

Not quite sure how to fix this. Max is a public variable and the two labels are pubic as well.

不太确定如何解决这个问题。 Max是一个公共变量,两个标签也是公共变量。

Thanks!

This is the code in my constructor (thank you lassevk for the code!):

这是我的构造函数中的代码(谢谢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();
}

2 个解决方案

#1


Is by any chance Form1 the name of the class?

Form1的名称是否有任何机会?

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.

由于okBtn不在同一表单上,因此您需要为MaxScore表单提供对Form1实例的引用。

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

例如,您可以将其添加到MaxScore表单中:

public Form1 MainForm { get; set; }

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

然后在你的okBtn_Click方法中,你会写下这个:

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):

然后当你从Form1构建MaxScore时(我假设你正在做的事情):

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

#2


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.

我同意@lassevk解决您的问题。我还建议将标签设置为Form1类中的方法的行为,这有助于保持代码更清晰,并保持更新哪些字段以及如何更新父表单中包含的字段的责任/知识。您只需在Form1中定义一个公共方法,该方法接受一个字符串值并使用该值更新特定标签。然后在MaxScore表单中,在按钮单击事件处理程序中,您将调用该方法而不是尝试直接访问这些标签控件。

Just food for thought.

只是值得深思。

#1


Is by any chance Form1 the name of the class?

Form1的名称是否有任何机会?

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.

由于okBtn不在同一表单上,因此您需要为MaxScore表单提供对Form1实例的引用。

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

例如,您可以将其添加到MaxScore表单中:

public Form1 MainForm { get; set; }

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

然后在你的okBtn_Click方法中,你会写下这个:

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):

然后当你从Form1构建MaxScore时(我假设你正在做的事情):

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

#2


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.

我同意@lassevk解决您的问题。我还建议将标签设置为Form1类中的方法的行为,这有助于保持代码更清晰,并保持更新哪些字段以及如何更新父表单中包含的字段的责任/知识。您只需在Form1中定义一个公共方法,该方法接受一个字符串值并使用该值更新特定标签。然后在MaxScore表单中,在按钮单击事件处理程序中,您将调用该方法而不是尝试直接访问这些标签控件。

Just food for thought.

只是值得深思。