:如何在没有代理的情况下在c#中创建事件?

时间:2017-07-26 11:10:57

标签: c# events

我试过这样但它会返回空值,

UserControl

如何实现事件火灾?

修改

我在该用户控件中有一个Form,其中包含按钮事件,在我创建此事件的ButtonClick方法中。

我有.table-fixed thead { width: 97%; } .table-fixed tbody { height: 230px; overflow-y: auto; width: 100%; } .table-fixed thead, .table-fixed tbody, .table-fixed tr, .table-fixed td, .table-fixed th { display: block; } .table-fixed tbody td, .table-fixed thead > tr> th { float: left; border-bottom-width: 0; } 。在那种形式我使用这个UserControl。所以我需要从用户控制按钮单击事件到表单页面特定功能来激活事件。

2 个答案:

答案 0 :(得分:0)

我为你写了一个非常简单,基本的解决方案。请阅读代码中的注释以了解解决方案。如果有任何不清楚的地方,请提出问题。

以下示例,如果此人的姓名发生变化,将导致事件触发:

这是Person对象:

/**
 - To store all colors
 - Naming Convention identifier: In lowerCamelCase with single name description
 - Naming Convention value: single name

 - example:  self.view.backgroundColor = UIColor.View.NonCompliant

 */
extension UIColor{

        /**
         - To specify all seperators' color
         */
        struct Seperators{
            static let commonSeperator = UIColor(red: 53.0/255.0, green: 126.0/255.0, blue: 167.0/255.0, alpha: 1.0)
        }
}

以下是实例化和使用Person对象的代码:

public class Person
{
    //Event to register to, when you want to capture name changed of person
    public event EventHandler<NameChangedEventArgs> nameChangedEvent;

    //Property
    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            this._name = value;
            //Call the event. This will trigger the OnNameChangedEvent_Handler
            OnNameChangedEvent_Handler(new NameChangedEventArgs() {NewName = value});
        }
    }

    private void OnNameChangedEvent_Handler(NameChangedEventArgs args)
    {
        //Check if event is null, if not, invoke it.
        nameChangedEvent?.Invoke(this, args);
    }
}

//Custom event arguments class. This is the argument type passed to your handler, that will contain the new values/changed values
public class NameChangedEventArgs : EventArgs
{
    public string NewName { get; set; }
}

答案 1 :(得分:0)

请你试试以下的召唤活动方式:

public event EventHandler myevent;
myevent += new  EventHandler(Button1_Click);

        if (myevent != null)  //Here I get null value.
            myevent(1, new EventArgs());