C# 对单个事件使用两个委托时订阅不正确

C# 对单个事件使用两个委托时订阅不正确,c#,events,delegates,C#,Events,Delegates,大家好,我在使用委托订阅同一事件时遇到了一些问题,包括两个表单(父表单和子表单),这是我的代码: public delegate void SplitDelegate(object s, ControllerEventArgs e); public delegate void SetBatchDelegate(object s, ControllerEventArgs e); public class ControllerEventArgs : EventArgs { public s

大家好,我在使用委托订阅同一事件时遇到了一些问题,包括两个表单(父表单和子表单),这是我的代码:

public delegate void SplitDelegate(object s, ControllerEventArgs e);
public delegate void SetBatchDelegate(object s, ControllerEventArgs e);

public class ControllerEventArgs : EventArgs
{
    public string sp1;
    public int ip1;
    public int ip2;

    public ControllerEventArgs(string sp1)
    {
        this.sp1 = sp1;
    }

    public ControllerEventArgs(int p1, int p2)
    {
        this.ip1 = p1;
        this.ip2 = p2;
    }

    public ControllerEventArgs(string sp1, int p1, int p2)
    {
        this.sp1 = sp1;
        this.ip1 = p1;
        this.ip2 = p2;
    }
}

public interface IController
{
    event SplitDelegate splitRowEvent;
    event SetBatchDelegate setSmallBatchEvent;
    void InvokeControllerEvent(string p1);
    void InvokeControllerEvent(int p1, int p2);
    void InvokeControllerEvent(string sp1, int p1, int p2);

}

public class Controller : IController
{
    public event SplitDelegate splitRowEvent;
    public event SetBatchDelegate setSmallBatchEvent;

    public void InvokeControllerEvent(string p1)
    {
        this.OnControllerEvent(new ControllerEventArgs(p1));
    }

    public void InvokeControllerEvent(int p1, int p2)
    {
        this.OnControllerEvent(new ControllerEventArgs(p1, p2));
    }

    public void InvokeControllerEvent(string sp1, int p1, int p2)
    {
        this.OnControllerEvent(new ControllerEventArgs(sp1, p1, p2));
    }

    protected virtual void OnControllerEvent(ControllerEventArgs e)
    {
        if (this.splitRowEvent != null)
        {
            this.splitRowEvent.Invoke(this.splitRowEvent, e);
        }
        if (this.setSmallBatchEvent != null)
        {
            this.setSmallBatchEvent.Invoke(this.setSmallBatchEvent, e);
        }
    }
}
例如,当我填写表格A时:

frmFormB.controller.InvokeControllerEvent("ugPER");
当我只想使用第一个
SplitRowEvent

有没有一种方法可以使用正确的语法重新生成代码,以获得正确的回调

为获得最佳解释,添加了:

按钮1\u单击:

SmallBatchSplitSetRows frmModal = new SmallBatchSplitSetRows(this.controller);
            frmModal.controller.InvokeControllerEvent("ugFAB");
            frmModal.ShowDialog();
按钮2\u单击:

 SmallBatchSplitInput frmModal = new SmallBatchSplitInput(this.controller);
            frmModal.controller.InvokeControllerEvent("ugPER");
            frmModal.ShowDialog();

您的
OnControllerEvent
方法显式调用使用
splitRowEvent
setSmallBatchEvent
。如果您不希望,只需更改方法的内容即可


最终,不清楚为什么有两个不同的事件,但只有一种调用它们的方法,即通过三个重载。这三个重载的作用是不同的吗?每个都是用来调用不同的事件处理程序集的吗?这就是使用描述性名称而不是重载将非常有用的地方。

Hi@Jon,是的,3个重载提供了3种不同的方式在表单之间传递参数,当我删除每个事件的显式使用时,我得到samething@AngelEscobedo当前位置但是为什么您首先有两个事件,您希望如何确定何时引发哪个事件?控制器如何知道必须调用哪个而不是另一个?一个代码片段将非常有用,谢谢,因为在我的父窗体中有两个事件需要区分,我会在不同的按钮点击中提出每个事件events@AngelEscobedo:不,你没有抓住重点-你需要说在什么情况下它应该引发什么事件。您已经声明了两个事件,并且有一个方法同时引发两个事件。你需要考虑为什么你有两个项目,以及何时你想提高每一个项目。我不可能为你做那件事——这是你的设计。