值更改时触发事件 C#

Sil*_*Bee 6 c# winforms

我正在尝试监视一个值,当它发生更改时,在使用结果执行一些计算后更新文本字段。

我试图监视的值来自一个AGauge属性(自定义控件)。我想在AGauge.Value更改时更新文本字段。

我看过诸如This One这样的问题,但我真的不明白这是如何工作的,或者我需要改变什么才能得到我正在寻找的结果。

谁能更好地解释我需要做什么才能使其发挥作用?

这AGuage.Value是一种float类型,以防万一你想知道。

提前致谢。

更新 1

我现在已将以下代码添加到我的项目中:

public class AGuage
{
    private float _value;

    public float Value
    {
        get
        {
            return this._value;
        }
        set
        {
            this._value = value;
            this.ValueChanged(this._value);
        }
    }

    public void ValueChanged(float newValue)
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

并且可以ValueChanged使用以下方法触发:

    AGuage n = new AGuage();

    n.Value = Pressure_Gauge.Value;
Run Code Online (Sandbox Code Playgroud)

每次Pressure_Gauge.Value更新时都会触发。

我现在面临的问题或最后一个障碍是这部分:

public void ValueChanged(float newValue)
{
  Form1.Pressure_Raw.text = "Working";
}
Run Code Online (Sandbox Code Playgroud)

我想form1使用上述方法更新标签的文本,但是我收到一条错误消息:An object reference is required for the nonstatic field, method, or property。

我不知道该怎么做,我已经阅读了一些关于静态属性的信息,但是我将如何在其中更新标签的文本值?

谢谢。

mik*_*ike 5

这可能会有所帮助。您可以添加一个事件并在表单中订阅它。

例如:

public class AGauge {

    // You can either set the Value this way
    public float Value {
        get {return this.Value;}
        set 
        {
             // (1)
             // set "Value"
             this.Value = value;
             // raise event for value changed
             OnValueChanged(null);
        }
    }

    // create an event for the value change
    // this is extra classy, as you can edit the event right
    // from the property window for the control in visual studio
    [Category("Action")]
    [Description("Fires when the value is changed")]
    public event EventHandler ValueChanged;

    protected virtual void OnValueChanged(EventArgs e)
    {
        // (2)
        // Raise the event
        if (ValueChanged != null)
            ValueChanged(this,e);
    }

}

public Form1 : Form {
    // In form, make your control and add subscriber to event 
    AGauge ag = new AGauge();
    // (3)
    ag.ValueChanged += UpdateTextBox;

    // (4)
    public void UpdateTextBox(object sender, EventArgs e) 
    {
        // update the textbox here
        textbox.Text = ag.Value;
    }
}
Run Code Online (Sandbox Code Playgroud)

其工作原理如下:在(3)处,您将订阅者添加到 ag.ValueChanged 事件,如此处所述。当您要更改 ag.Value 时,您会到达(1),其中 Value 被更改并调用 OnValueChanged 。这会将您带到(2),其中引发 ValueChanged 事件。发生这种情况时,该事件的所有订阅者都会收到“通知”并调用各自的方法。因此,当您到达(2)时,(4)最终会被调用,因为“UpdateTextBox”被设置为 ValueChanged 事件的订阅者。这有点棘手,但非常有用。

或者,如果您想继续尝试执行此操作,则需要执行以下操作:

public class AGuage
{
    private float _value;

    // create object of Form1 for reference
    private Form1 form1;

    // pass reference to form1 through constructor
    public AGauge(Form1 form1)
    {
        // assign
        this.form1 = form1;
    }

    public float Value
    {
        get
        {
            return this._value;
        }
        set
        {
            this._value = value;
            this.ValueChanged(this._value);
        }
    }

    public void ValueChanged(float newValue)
    {
        // use the form1 reference
        this.form1.Pressure_Raw.Text = "Working";
    }
}
Run Code Online (Sandbox Code Playgroud)

然后这样做:

// if creating the AGauge object in Form1, pass "this" to the object
AGuage n = new AGuage(this);
Run Code Online (Sandbox Code Playgroud)

我强烈建议您不要这样做,因为这违反了 OOP 的泛型规则。这意味着,如果您尝试在 Form1 以外的其他任何地方使用此 AGauge 控件,它将不会以相同的方式工作。我建议对如上所述的事件进行此操作。它更加通用。