【问题标题】:Base Class interacting with Derived class Controls in Winforms C#基类与 Winforms C# 中的派生类控件交互
【发布时间】:2011-09-17 23:23:57
【问题描述】:

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

【问题讨论】:

    标签: c# winforms base-class


    【解决方案1】:

    看看this question & answers - 这应该可以满足您的需求,尽管需要进行一些调整。

    【讨论】:

      【解决方案2】:

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

      你应该这样做

      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);
         }
      }
      

      【讨论】:

      • 我不知道winforms中存在这种方法,谢谢!我知道如何检查 Form.controls 但由于您所说的原因,我得到了 0 个控件。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-17
      • 2010-10-21
      • 2017-11-16
      • 1970-01-01
      • 1970-01-01
      • 2013-03-29
      • 2016-05-15
      相关资源
      最近更新 更多