1

在 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 的所有属性、方法和事件在设计器中保持公开。除了编写所有的获取/设置/事件/方法等之外,我不知道公开它们的“简单”方法(即将子控件方法等公开给父用户控件)。

我想我可以改变继承: public partial class MyDataGridView : UserControl To: public partial class MyDataGridView : DataGridView

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

所以....谁能告诉我如何解决这个问题?

编辑:顺便说一句......我知道子类化将是:

public partial class MyDataGridView : DataGridView

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

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

4

2 回答 2

1

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

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!");
    }
}
于 2013-05-28T23:29:29.443 回答
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!");
        }
    }
于 2013-05-30T01:38:01.170 回答