简体   繁体   English

像Java一样的监听器C#

[英]listener c# like java

I have field string in struct, and i want learn real-time changed this field. 我在struct中有字段字符串,我想学习实时更改此字段。

struct example {
public string ex;
}

examp = new example();<BR>
examp.ex = "test";

////// then program work and eamp.ex = "bing";

I need method 我需要方法

on_ex_changed() 
{
    if examp.ex changed then ..... 
}

online and simple plz 在线和简单的PLZ

You can put an event at the setter as follows. 您可以按照以下步骤在设置器上放置一个事件。 The event will be fired every time the setter is called. 每次调用设置器时都会触发该事件。

public class MyObj
{
    private RectangleF mRectangle;

    public event EventHandler RectangleChanged;

    public RectangleF Rectangle
    {
        get
        {
            return mRectangle;
        }

        set
        {
            mRectangle = value;
            OnRectangleChanged();
        }
    }

    protected virtual void OnRectangleChanged()
    {
        if (RectangleChanged != null)
        {
            RectangleChanged(this, EventArgs.Empty);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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