如何向控件添加_Load事件 - 扭曲

时间:2013-05-28 23:23:25

标签: c# user-controls

在C#中,我想将一个_ResizeEnd事件添加到DataGridView控件。我找到了一些代码来帮助解决这个问题(允许向用户控件添加_ResizeEnd事件)。

private void UserControl1_Load(object sender, EventArgs e)
{
((Form)this.Parent).ResizeEnd += new EventHandler(UserControl1_ResizeEnd);
}
void UserControl1_ResizeEnd(object sender, EventArgs e)
{
MessageBox.Show("Resize end");
}

如上所述,我想调整它以将事件添加到DataGridView。我能做的是创建一个UserControl并将DataGridView控件转储到它上面,并按照上面的代码实现_ResizeEnd事件。

然而,问题在于我希望DataGridView的所有属性,方法和事件在设计器中保持公开。除了编写所有Get / Set / events / methods等之外,我不知道暴露它们的“简单”方法(即将子控件方法等暴露给父用户控件)。

我以为我可以改变继承:     public partial class MyDataGridView:UserControl 至:     public partial class MyDataGridView:DataGridView

这解决了将所有DataGridView属性等暴露给usercontrol的问题,但当然这不会让我前进,因为DataGridView类(与UserControl类不同)没有_Load事件。 / p>

因此.... 有谁能告诉我如何解决这个问题?

编辑: 顺便说一句......我明白SubClassing会是:

public partial class MyDataGridView : DataGridView

这确实暴露了DataGridView属性等,但是我丢失了:UserControl继承,这意味着没有暴露_Load事件。

我不确定如何继承UserControl属性/方法和DataGridView属性等。

2 个答案:

答案 0 :(得分:1)

为什么必须在ResizeEnd事件中设置Load?为什么不将DataGridView子类化(这是获取所有现有属性和事件的最佳方法),然后在MyDataGridView中设置事件处理程序?由于你想要的只是父母,我建议你对ParentChanged事件作出反应。以下对我有用(请注意,我不相信父母会改变,但人们可以做一些时髦的事情:)):

public class CustomDataGridView : DataGridView
{
    private Form _curParent;

    public CustomDataGridView()
    {
        // Since Parent is not set yet, handle the event that tells us that it *is* set
        this.ParentChanged += CustomDataGridView_ParentChanged;
    }

    void CustomDataGridView_ParentChanged(object sender, EventArgs e)
    {
        if (this.Parent is Form)
        {
            // be nice and remove the event from the old parent
            if (_curParent != null)
            {
                _curParent.ResizeEnd -= CustomDataGridView_ResizeEnd;
            }

            // now update _curParent to the new Parent
            _curParent = (Form)this.Parent;

            _curParent.ResizeEnd += CustomDataGridView_ResizeEnd;
        }
    }

    void CustomDataGridView_ResizeEnd(object sender, EventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("Resize End called on parent. React now!");
    }
}

答案 1 :(得分:0)

好的 - 谢谢你的帮助。最终的工作代码如下(如果需要改进,可以接受建议!)

 public partial class MyDataGridView : DataGridView
    {
        private Form _curParent = null;
        protected override void OnInvalidated(InvalidateEventArgs e) 
        {
            //Exit if no parent, or _curParent already set.
            if (Parent == null || _curParent != null) return;

            base.OnInvalidated(e);

            //Recurse until parent form is found:
            Control parentForm = Parent;

            while (!(parentForm is Form))
            {
                if (parentForm.Parent == null) return;  //Break if this is a null - indicates parent not yet created.
                parentForm = parentForm.Parent; 
            }

            //Have now found parent form at the top of the ancestor tree.
            // be nice and remove the event from the old parent
            if (_curParent != null)
            {
                _curParent.ResizeEnd -= MyDataGridView_ResizeEnd;
            }
            // now update _curParent to the new Parent
            _curParent = (Form)parentForm;

            //Add the resized event handler
            _curParent.ResizeEnd += MyDataGridView_ResizeEnd;

        }


        void MyDataGridView_ResizeEnd(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Resize End called on parent. React now!");
        }
    }