gpt4 book ai didi

c# - 如何从添加到另一个控件的子控件访问变量?

转载 作者:太空宇宙 更新时间:2023-11-03 17:24:28 26 4
gpt4 key购买 nike

我有以下代码(这是我试图在其中包含所有重要信息的摘录)

public partial class MyTopScreen : Form
{
public bool _myVariable ;
public CommonForm _commonGridForm;
public MyTopScreen(bool first_param, string second_param )
{
CommonForm commonGridForm = new CommonForm();
DataGridView dataGridView1 = commonGridForm.dataGridView1;
_commonAttributForm = commonAttributForm;
InitializeComponent();
this.TabPage1.Controls.Add(dataGridView1);
}
}

然后当我进入 CommonForm 时,如下所示:

public partial class CommonForm : Form        
{
public CommonForm()
{
//My Question is how do I access the _myVariable from this point in the code ?
//this.parent doesnot work or does not give me access to _myVaribale
}
}

在此先感谢您的大力帮助。

最佳答案

一种方法,将 MyTopScreen 的实例传递给其他类的构造函数(如果相关)。将其存储在字段中。

public partial class CommonForm : Form
{
public CommonForm(MyTopScreen screen)
{
this.TopScreen = screen;
}

private MyTopScreen TopScreen{ get; set; }
}

现在您可以通过 TopScreen._myVariable.CommonForm 中访问该变量。

你必须将它传递给构造函数:

CommonForm commonGridForm = new CommonForm(this);

关于c# - 如何从添加到另一个控件的子控件访问变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27444829/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com