访问两个表单之间的EventHandler

时间:2010-11-07 14:13:10

标签: c# winforms event-handling

我想在Form2中访问Form1的EventHandler

Form1 EventHandler : -

 private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)

如何实现它?。

4 个答案:

答案 0 :(得分:3)

你做错了什么。

如果要公开功能,则应创建一个公共方法/函数来执行此操作。您可以从其他表单的事件处理程序中调用它。

答案 1 :(得分:1)

在评论中通过您的问题更新了答案,我没有检查它是否正常工作可能是有错误的: 当你有类似的事件时它很有用,你也可以传递不同的EventArgs,最简单的方法就是拥有一个不同的Property来确定每个表单并在set方法中添加事件但是下面是一般的

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)
    {

    }
}    

答案 2 :(得分:0)

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

答案 3 :(得分:0)

另一种选择是将Form1上的事件处理程序解析为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; }
    }
}

但这需要Form1有权访问Form2