假设我有一个从“Form”继承的 FormBase 类,并且我有从 FormBase 继承的 winforms 表单,我如何访问和操作子表单中的控件,如下所示:
public class FormBase : Form
{
protected FormBase()
{
//for each Control in Child form Controls
//Do something with the Controls
}
}
public partial class Products : FormBase
{
public Products()
{
InitializeComponent();
}
}
最佳答案
您不应在基本表单的构造函数中访问子表单的控件。因为基构造函数将首先运行,然后子构造函数运行。
相反,你应该这样做
public class FormBase : Form
{
protected override void OnLoad(EventArgs e)
{
//access the child controls here. Take a look at Will A's answer
base.OnLoad(e);
}
}
关于c# - Winforms C# 中基类与派生类控件交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6399496/