Base Class interacting with Derived class Controls in Winforms C#
Lets Say I have a F开发者_开发知识库ormBase Class that is inherited from 'Form', and I have winforms Form that inherit from FormBase, how do I get access and manipulate the Controls in the Child Form as follows:
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();
}
}
You should not access the controls of the child form in the constructor of the base form. Because base constructor will be run first and child constructor after that.
Instead you should do
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);
}
}
Take a look at this question & answers - this should give you what you need, albeit with some adaptation.
精彩评论