2

假设我有一个从“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();            
         }
    }

4

2 回答 2

3

您不应在基本表单的构造函数中访问子表单的控件。因为将首先运行基本构造函数,然后再运行子构造函数。

相反,你应该做

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);
   }
}
于 2011-06-19T00:51:07.497 回答
0

看看这个问题和答案- 这应该给你你需要的东西,尽管有一些适应。

于 2011-06-19T00:21:28.187 回答