Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何向控件添加_Load事件-带有扭曲_C#_User Controls - Fatal编程技术网

C# 如何向控件添加_Load事件-带有扭曲

C# 如何向控件添加_Load事件-带有扭曲,c#,user-controls,C#,User Controls,在C#中,我想向DataGridView控件添加一个_ResizeEnd事件。我找到了一些代码来帮助实现这一点(它允许向usercontrol添加_ResizeEnd事件) 如前所述,我想对此进行调整,以将事件添加到DataGridView。我可以做的是创建一个UserControl并将DataGridView控件转储到它上面,然后按照上面的代码实现_ResizeEnd事件 但是,问题是我希望DataGridView的所有属性、方法和事件都保持在设计器中公开。除了编写所有Get/Set/even

在C#中,我想向DataGridView控件添加一个_ResizeEnd事件。我找到了一些代码来帮助实现这一点(它允许向usercontrol添加_ResizeEnd事件)

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

但是,问题是我希望DataGridView的所有属性、方法和事件都保持在设计器中公开。除了编写所有Get/Set/events/methods等,我还不知道有一种“简单”的方法可以公开它们(即,将子控件方法等公开给父用户控件)

我想我可以从以下方面更改继承: 公共部分类MyDataGridView:UserControl 致: 公共部分类MyDataGridView:DataGridView

这解决了将所有DataGridView属性等公开给usercontrol的问题,但这当然不会让我前进,因为DataGridView类(与usercontrol类不同)没有加载事件

所以。。。。 有人能告诉我如何解决这个问题吗

编辑: 顺便说一句我理解子类化将是:

public partial class MyDataGridView : DataGridView
这确实公开了DataGridView属性等,但我丢失了:UserControl继承,这意味着没有公开_Load事件


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

为什么必须在
加载
事件中设置
ResizeEnd
?为什么不将
DataGridView
子类化(这是获取所有现有属性和事件的最佳方式),然后在
MyDataGridView
中设置事件处理程序?因为您只需要父对象,所以我建议您对
ParentChanged
事件做出反应。下面的内容对我很有用(请注意,我不相信父母会改变,但人们可以做一些古怪的事情:):


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


好的,谢谢你的帮助。最终的工作代码如下(如果需要改进,欢迎建议!)


好的,谢谢你的帮助。最终的工作代码如下(如果需要改进,欢迎建议!)


如果您想像这里一样了解父级,那么您需要的是ParentChanged事件,而不是Load事件。问题已解决。如果您想了解有关父级的信息,就像您在这里所做的那样,那么您需要的是ParentChanged事件,而不是Load事件。问题已解决。据我所知(我不是专家!),如果将其放入构造函数中,则问题发生在调用构造函数时未加载控件,因此它没有可以捕获
ResizeEnd
事件的父级(窗体)。通过使用
load
事件,控件将有一个父控件。感谢您提供有关子类化的信息。不确定如何执行此操作(以前仅在Visual Basic 6中执行过此操作)。我会研究的。好的,谢谢。虽然我发现了另一个问题,但该代码工作得很好。。。当
CustomDataGridView
直接放在表单上时,它可以很好地工作。当控件放置在窗体上的另一个容器中时,它不起作用。我尝试使用递归方法来迭代父层次结构,以在树的顶部找到表单,但由于某种原因,这不起作用,因为在发生
ParentChanged
事件时,父级显然为空(见图…
,而(!(parentForm是Form)){parentForm=parentForm.parent;}
(很抱歉,这些注释中没有漂亮的格式!)由于放置顺序的原因,它将不起作用。首先将
网格添加到容器中,然后将该容器添加到表单中。2个选项:1.将处理程序添加到父级的父级,直到到达表单为止(总类)2.使用不同的回调。例如,删除对ParentChanged的回调,改为使用:protected override void OnInvalidated(InvalidateEventArgs e){base.OnInvalidated(e);}在其中查找(并缓存!否则您的性能会受到影响)父级并设置处理程序。据我所知(我不是专家!),如果你把它放在构造函数中,那么问题是调用构造函数时没有加载控件,因此它没有父控件(表单)您可以从中捕获
ResizeEnd
事件。通过使用
load
事件,控件将有一个父控件。感谢提供有关子类化的信息。不确定如何执行此操作(以前仅在Visual Basic 6中执行过此操作)。我会研究它。好的-谢谢。虽然我发现了另一个问题,但该代码工作得很好…当
CustomDataGridView
直接放置到表单上时,它工作得很好。当控件放置在表单上的另一个容器中时,它不工作。我尝试使用递归方法迭代父层次结构以查找Form位于树的顶部,但由于某些原因,这不起作用,因为在发生
ParentChanged
事件时,父级显然为空(见图…
,而(!(parentForm is Form)){parentForm=parentForm.Parent;}
(很抱歉,这些注释中没有很好的格式!)由于位置的顺序,它将不起作用。首先将
网格添加到容器中,然后将该容器添加到表单中。2个选项:1.将处理程序添加到ParentCh
public partial class MyDataGridView : DataGridView
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!");
    }
}
 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!");
        }
    }