Trigger event when C # value changes

I try to control the value and when changing it, update the text field after performing some calculations with the result.

The value I'm trying to control comes from a property AGauge(custom control). I want to update the text box when changing AGauge.Value.

I covered issues like This One , but I really don't understand how it works, or what I need to change to get the result. m looking for.

Could someone better explain what I need to do to make this work?

AGuage.Value- this is the type float, you may be surprised.

Thanks in advance.

Update 1

Now I have added the following code to my project:

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

    }
}

And you can start ValueChangedusing the following:

    AGuage n = new AGuage();

    n.Value = Pressure_Gauge.Value;

Pressure_Gauge.Value.

, , , - :

public void ValueChanged(float newValue)
{
  Form1.Pressure_Raw.text = "Working";
}

form1 , : An object reference is required for the nonstatic field, method, or property.

, , Static, ?

.

+4
4

. .

:

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;
    }
}

: (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";
    }
}

:

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

, generics OOP. , AGauge , Form1, . , . .

+4

AGauge INotifyPropertyChanged Value. Google , , StackOverflow.

Binding AGauge. , , , .

:

var binding = new Binding("Text", myAgaugeControl, "Value");
binding.Format += BindingFormat;
binding.Parse += BindingParse;
myTextBox.DataBindings.Add(binding);

BindingFormat BindingParse . Format . :

void BindingFormat(object sender, ConvertEventArgs e)
{            
    e.Value = e.Value.ToString();
}

BindingParse : , AGauge. .

Binding, Format Parse

+1

, Value. , , hook, ValueChanged(). , .

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)
    {
        // Action to perform on value change
        // Update a text field after performing some calculations with a result.
    }
}
+1

Microsoft Reactive Framework (NuGet "Rx-WinForms" ). ( ) LINQ.

:

public class AGuage
{
    private float _value;
    private Subject<float> _values = new Subject<float>();

    public float Value
    {
        get { return _value; }
        set
        {
            _value = value;
            _values.OnNext(value);
        }
    }

    public IObservable<float> Values
    {
        get { return _values.AsObservable(); }
    }
}

:

var aGuage = new AGuage();

var query =
    from value in aGuage.Values
    where value > 5.0f && value < 20.0f //filtering
    select value * 150f + 45.3f; //computation

var subscription =
    query.Subscribe(value =>
    {
        /* do something with the filtered & computed value */
    });

aGuage.Value = 2.1f; // query.Subscribe doesn't fire
aGuage.Value = 12.4f; // query.Subscribe DOES fire
aGuage.Value = 202.1f; // query.Subscribe doesn't fire

If you want to close the subscription to the values, just call subscription.Dispose().

0
source

Source: https://habr.com/ru/post/1625826/


All Articles