简体   繁体   English

C#winforms / UserControls - 如何从UserControl1触发事件到UserControl2

[英]C# winforms / UserControls - How to trigger Event from UserControl1 to UserControl2

I am trying to trigger a Button1_Click event located in UserControl2 from UserControl1. 我试图从UserControl1触发位于UserControl2中的Button1_Click事件。

Controls are Loaded dynamically from my Main Form 控件是从我的主窗体动态加载的

//UserControl2
public partial  class ItemsModule : UserControl
{
        private void ButtonRefreshProperties_Click(Object sender, EventArgs e)
        {
            RefreshControls();
        }
        public void RefreshControls()
        {
            SomeCode();
        }
}

/// MainForm this is how i add the Control from the Main Form

ItemsModule im = new ItemsModule();
im.Name = "ItemsModule";
flpModules.Controls.Add(im);

//UserControl1
public partial class TreeViewControl :  UserControl
{
    private void ItemTreeView_MouseDoubleClick(Object sender, MouseEventArgs e)
    {
        String ItemId = ItemTreeView.SelectedNode.Name;
        Variables.CurrentItemID = ItemId;
        if (LoadedModules.Items)
        {
            //Here I would like a way to trigger ItemsModule.ButtonRefreshProperties_Click
        }
    }

}  

UserControl1 will need to have a references to UserControl2 . UserControl1需要具有对UserControl2的引用。 Once you have a reference UserControl1 should call a method on UserControl2 that causes the event to be raised. 一旦有了引用, UserControl1应该在UserControl2上调用一个导致事件被引发的方法。

You can pass the reference to UserContro2 in to UserControl1 via the constructor, a sub, or a property depending on how your application is structured. 您可以通过参考UserContro2UserControl1通过构造,子,或者根据您的应用程序是如何组织的属性。 You should store the reference in a private field of UserControl1 so that you can access it from the DoubleClick event handler. 您应该将引用存储在UserControl1的私有字段中,以便您可以从DoubleClick事件处理程序访问它。

It is also possible to set this up in other ways. 也可以通过其他方式进行设置。 You could make UserControl2 be a property on the parent form, then UserControl1 can cast it's .Parent property in to the correct type and use that property as a reference. 您可以将UserControl2设置为父窗体上的属性,然后UserControl1可以将其.Parent属性.Parent转换为正确的类型,并将该属性用作引用。

You could also set up a 3rd class as a singleton that has a reference to UserControl2 . 您还可以将第3类设置为具有UserControl2引用的singleton

The thing that all these solutions have in common is that somehow UserControl1 needs to have a reference to UserControl2 to be able to do anything with it. 所有这些解决方案的共同之处在于, UserControl1需要以某种方式引用UserControl2才能对其执行任何操作。

You could avoid the reference problem by having UserControl1 raise an event that it's parent form listens for. 您可以通过让UserControl1引发其父窗体侦听的事件来避免引用问题。 Since the parent form should already have a reference to all the controls on it, you can make the form proxy the calls in to UserControl2 for you. 由于父表单应该已经引用了它上面的所有控件,因此您可以让表单代理调用UserControl2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM