简体   繁体   English

C#从父窗体的2个方法访问子窗体的相同实例

[英]C# Accessing the same instance of a child form from 2 methods in the parent form

So far i have this code that i want to use to be able to call the same function in the child form. 到目前为止,我已经有了此代码,希望能够以子窗体形式调用相同的函数。

Parent Form Code: 家长表格代码:

FormGame frmGame; // i put this here so that in the second function it doesnt complain about frmGame not being set.
    public void CreateGame(string Level) // this function is called first
    {
        FormGame frmGame = new FormGame(this); // i need both functions to be able to access this instance of the child form
        frmGame.SetLevel(Level); // sets the text of a label in the child form
        frmGame.Show();
    }

    public void UpdateGame(string Level) // then this function is called second 
    {
        frmGame.SetLevel(Level); // to update the same label as set in the first method
    }

the problem with this code is that while yes it doesn't come up with any errors when just sitting there but when debugging it when the second function is called it cant find the instance of FormGame that was set in the first function so frmGame is null. 这段代码的问题是,虽然是的,但是当它坐在那里时并没有出现任何错误,但是在调用第二个函数时对其进行调试时,它找不到在第一个函数中设置的FormGame实例,因此frmGame为null 。

I have tried: 我努力了:

  1. Pulling the whole form initializing statement outside both functions like this 像这样在两个函数之外拉出整个表单初始化语句
    FormGame frmGame = new FormGame(this); FormGame frmGame =新的FormGame(this);
    but it doesnt like "this" not being inside a function and removing it from that line then removes the errors when running but then the label never changes when told to 但是它不喜欢“ this”不在函数内部并从该行中删除,然后删除运行时的错误,但是当告诉
  2. what is shown in the code at the top 顶部代码中显示的内容
  3. Having
    FormGame frmGame = new FormGame(this); FormGame frmGame =新的FormGame(this); At the top line inside of both of the functions but that just resets the form back to the initial one every time I try update it 在这两个函数的顶行,但是每次我尝试更新表单时,它都会将表单重置为初始表单
  4. Tried using Refresh() in the second function and inside the child form after the label text gets changed but to no avail. 标签文本更改但无济于事后,在第二个函数中以及子窗体内部使用Refresh()进行了尝试。
  5. And a few more but they were way off what i feel is correct. 还有一些,但它们与我认为正确的相去甚远。

    So my goal is to be able to create the form in the first function and set the label on it and then when the second function is called with a new string I want to be able to update that same label without closing the open form. 因此,我的目标是能够在第一个函数中创建表单并在其上设置标签,然后在使用新字符串调用第二个函数时,我希望能够在不关闭打开的表单的情况下更新同一标签。

Your code creates a new instance of FormGame , whose scope is only inside that function. 您的代码创建一个FormGame的新实例,其范围仅在该函数内部。 In no way does this affect the frmGame variable you defined outside of the method. 这绝不会影响您在方法外部定义的frmGame变量。

FormGame frmGame = new FormGame(this);

To avoid getting an error when you call UpdateGame , don't define a new variable inside the method. 为避免在调用UpdateGameUpdateGame ,请不要在方法内部定义新变量。

public void CreateGame(string Level)
{
   frmGame = new FormGame(this);  // use the class-level field
   ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM