简体   繁体   中英

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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