c# - Winforms C# 中基类与派生类控件交互

标签 c# winforms base-class

假设我有一个从“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/

相关文章:

vb.net - AxAcroPdf 加载后,执行某些操作

c# - 如何在目标线程上运行事件处理程序

c# - 基类中的模板方法有什么用?

c# - 处理数据库数据时 xUnit 测试死锁

c# - Google Sheets API v4 dot Net 删除一行

c# - 问题: Multithread bulkinsert

c++ - 为什么对基类子对象有限制?

c# - 如何创建可枚举/类字符串或 int 并序列化?

c# - DataGridView 中的 CellClick 事件和 SelectionChanged 事件

c# - 使用基类并拥有从其继承的类