简体   繁体   中英

Access EventHandler Between Two Forms

I wants to access Form1's EventHandler In Form2

Form1 EventHandler is :-

 private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)

How to achieve it?.

You are doing something wrong.

If you want to expose functionality, you should create a public method/function to do so. You can call this from your event handler and from your other form.

Answer updated by your question in comment, I didn't checked that it works fine may be there is a bug with it: It's useful when you have a similar event, Also you can pass different EventArgs, easiest way is to have a different Property which determines each form and add event in their set methods but bellow is general

public abstract class FormBase : Form
{
    public virtual event EventHandler MyEventHandler;
}

public class Form3 : FormBase
{
    public override event EventHandler MyEventHandler;
    Form2 instance;
    public Form3()
    {
        instance = Form2.Instance;
        instance[this.GetType().ToString()] = this;
        // or 
        //instance["Form3"] = this;
    }

    private void dataGridView1_CellEndEdit(object sender, EventArgs e)
    {
        // todo
        if (MyEventHandler != null)
            MyEventHandler(this, e);
    }
}

public class Form2
{
    Dictionary<string, FormBase> dic = new Dictionary<string,FormBase>();

    public FormBase this[string index]
    {
        get 
        { 
            FormBase retVal = null;
            if (dic.TryGetValue(index, out retVal))
                return retVal;
            return null;
        }
        set
        {
            FormBase retVal = null;

            if (value == null)
                return;

            if (dic.TryGetValue(index, out retVal))
            {
                try
                {
                    value.MyEventHandler -= MyEventHandler1;
                }
                catch
                { 
                }

                retVal = value;

                retVal.MyEventHandler += MyEventHandler1;
                return;
            }

            value.MyEventHandler += MyEventHandler1;

            dic.Add(index, value);
        }
    }

    private static Form2 instance;
    public static Form2 Instance
    {
        get
        {
            if (instance == null)
            { 
                instance = new Form2();
            }
            return instance;
        }
    }

    private Form2()
    {
    }

    private void MyEventHandler1(object sender, EventArgs e)
    {

    }
}    

将访问修饰符更改为public而不是private。

Another option is to parse the eventhandler on Form1 to Form2 :

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        DataGridViewCellEventHandler dataGridViewCellEventHandler = new DataGridViewCellEventHandler(this.dataGridView1_CellEndEdit);
        this.dataGridView1.CellEndEdit += dataGridViewCellEventHandler;

        Form2 form2 = new Form2();
        form2.CellEndEdit += dataGridViewCellEventHandler;
    }

    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        //Do something
    }
}

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    public event DataGridViewCellEventHandler CellEndEdit
    {
        add { dataGridViewOnForm2.CellEndEdit += value; }
        remove { dataGridViewOnForm2.CellEndEdit -= value; }
    }
}

This does however require that Form1 has access to Form2

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