简体   繁体   English

如何使用委托调用另一个类的方法?

[英]How to call a method on another class using delegates?

Let's say I have two forms.假设我有两种形式。 When I press a button on the first one, the second one is shown.当我按下第一个按钮时,会显示第二个按钮。 However I want to be able to close the second one by pressing a button on it BUT IT SHOULD FIRE A METHOD on the first one which is responsible of closing the second window.但是,我希望能够通过按下它上的按钮来关闭第二个,但它应该在第一个上触发一种方法,该方法负责关闭第二个窗口。 It has to be done using delegates and events.它必须使用委托和事件来完成。 How can I do it?我该怎么做? Thanks for the help.谢谢您的帮助。

your question is not so much clear.. may be it helps you.你的问题不是很清楚..可能对你有帮助。

 // Form1 is the 1st form or main form
 public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();

            //creat a sample object of FormEXt
            var form = new FormExt();
            //subscribe its CustomClose Event
            form.CustomClose += new FormExt.CloseMeDelegate(form_CustomClose);
            //set the text
            form.Text = "Samplee 01";
            //show the form
            form.Show();

            //repeat a again 
            form = new FormExt();
            form.CustomClose += new FormExt.CloseMeDelegate(form_CustomClose);
            form.Text = "Samplee 02";
            form.Show();
        }

        void form_CustomClose(Form form)
        {
            //get the form show the titile
            MessageBox.Show("Going to Close" + form.Text);
            //close the form
            form.Close();
        }
    }

     //second form with a button 
    public class FormExt : Form 
    {
        //creat a delegate which takes form as input
        public delegate void CloseMeDelegate(Form form);

        //creat event from delegeate set it fake delegate in order to avoid null excpetion
        public event CloseMeDelegate CustomClose = delegate { 

        };

        public FormExt() {

            //creat a button
            var button = new Button() { Text = "Click" , Top = 10 , Left=10 };

            //grab button click event
            button.Click += delegate
            {
                //invoke custome close event
                CustomClose(this);
            };
            //add button on the form
            this.Controls.Add(button);

        }

    }

In your second window, declare an event that is raised whenever the window wants to issue to be closed:在您的第二个窗口中,声明一个在窗口想要关闭时引发的事件:

// ## Second window ##
public delegate void IssueCloseDelegate(object sender, EventArgs e);
public event IssueCloseDelegate IssueClose;

When the close button is pressed, raise this event:当按下关闭按钮时,引发此事件:

// ## Second window, button click handler
if (IssueClose != null)
    IssueClose(this, EventArgs.Empty);

When you instantiate the window in the first window, register the event and handle it:在第一个窗口中实例化窗口时,注册事件并处理它:

// ## First window ##

    //some method
    var newWindow = new SecondWindow();
    newWindow.IssueClose += NewWindow_IssueClose;
    newWindow.Show();
    //...

private void NewWindow_IssueClose(object sender, EventArgs e)
{
    var closedWindow = (SecondWindow)sender;
    closedWindow.Close();
}

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

相关问题 使用委托从另一个类调用主类函数 - call a main class function from another class using delegates 如何在不使用委托的情况下将方法作为参数传递并立即调用它? - How to pass a method as a parameter without using delegates and call it instantly? 如何管理使用任意调用 arguments 注册的方法列表作为委托,通过另一个调用方法调用它们? - How to manage a list of methods registered with arbitrary call arguments count as delegates, to call them via another invocation method? 如何在不重复测试的情况下测试将大部分行为委派给同一类中另一方法的方法? - How should I test a method that delegates most of its behavior to another method in the same class without duplicating tests? MOQ - 如何验证静态类调用和委托? - MOQ - How to verify static class call and delegates? 如何调用要在另一个类的方法中使用的类 - How To Call A Class To Be Used In A Method Of Another Class 如何从另一个类调用ViewModel方法 - How to call a viewmodel method from another class 如何从另一个appDomain调用类的方法 - how to call a method of a class from another appDomain 如何从另一个类调用IWebElement方法? - How to Call IWebElement method from another class? 如何使用 SyncFusion Blazor 调用某个类中另一个类的方法来刷新组件? - How to call a method of another class in some class using SyncFusion Blazor to refresh a component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM