Carlo
Carlo

Reputation: 151

Set control value on leave or value changing

It's hard to explain problem when I don't know what causes this behavior but I will try.

On my form have two Devexpress SpinEdit controls (The SpinEdit class represents a control that consists of a text editor and a pair of spin buttons that a user can click to adjust a value just like using NumericUpDown)

The problem is that I cant set value of second spin edit after handling spin edit 1 value changing event.

When form load's both controls get values from database (decimal 18,2)

let say :

priceSpinEdit value is 100,00

discountSpinedit value is 10,00

When user change discount percentage I need to calculate price with discount and put value into priceWithDiscountSpinEdit

I have code that work's well

    private void supplierDiscountPercetangeSpinEdit_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
    {
        decimal price, percentage, calculatedPrice;

        price = (decimal)supplierPriceSpinEdit.EditValue;
        percentage = Convert.ToDecimal(e.NewValue, CultureInfo.InvariantCulture);

        calculatedPrice = Popust.Izracunaj(price, percentage);

        supplierPriceWithDiscountSpinEdit.EditValue = calculatedPrice;

    }

While focus is set on control in which I'm making calculation supplierPriceWithDiscountSpinEdit show correct calculated value but second after this control loses focus value is set to value loaded from database.

I was thinking that I need to write value to object but then i tried to put this into simple button click and it work's (value stick's)

    private void button1_Click(object sender, EventArgs e)
    {
        decimal price, percentage, calculatedPrice;

        price = (decimal)supplierPriceSpinEdit.EditValue;
        percentage = (decimal)supplierDiscountPercetangeSpinEdit.EditValue;

        calculatedPrice = Popust.Izracunaj(price, percentage);

        supplierPriceWithDiscountSpinEdit.EditValue = calculatedPrice;
    }

So my question is why when calculating price correct value is shown in second spinEdit and after losing focus it disappears.

Sorry for long question, and if necessary I can add more information.

EDIT : spinedit control

        // 
        // supplierPriceWithDiscountSpinEdit
        // 
        this.supplierPriceWithDiscountSpinEdit.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this.productBindingSource, "SupplierPriceWithDiscount", true));
        this.supplierPriceWithDiscountSpinEdit.EditValue = "0,00";
        this.supplierPriceWithDiscountSpinEdit.EnterMoveNextControl = true;
        this.supplierPriceWithDiscountSpinEdit.Location = new System.Drawing.Point(527, 33);
        this.supplierPriceWithDiscountSpinEdit.MenuManager = this.barManager1;
        this.supplierPriceWithDiscountSpinEdit.Name = "supplierPriceWithDiscountSpinEdit";
        this.supplierPriceWithDiscountSpinEdit.Properties.Mask.BeepOnError = true;
        this.supplierPriceWithDiscountSpinEdit.Properties.Mask.EditMask = "n2";
        this.supplierPriceWithDiscountSpinEdit.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric;
        this.supplierPriceWithDiscountSpinEdit.Properties.Mask.UseMaskAsDisplayFormat = true;
        this.supplierPriceWithDiscountSpinEdit.Properties.NullText = "0,00";
        this.supplierPriceWithDiscountSpinEdit.Properties.ValidateOnEnterKey = true;
        this.supplierPriceWithDiscountSpinEdit.Size = new System.Drawing.Size(100, 20);
        this.supplierPriceWithDiscountSpinEdit.TabIndex = 13;

Upvotes: 0

Views: 4500

Answers (1)

SidAhmed
SidAhmed

Reputation: 2352

Try to use DataSourceUpdateMode.OnPropertyChanged datasource update mode in your binding, like so :

supplierPriceWithDiscountSpinEdit.DataBindings.Add("EditValue", productBindingSource, "SupplierPriceWithDiscount", true, DataSourceUpdateMode.OnPropertyChanged);

And in the SpinEdit EditValueChanging event handler, change the object property, and let the binding do it's work.

private void supplierDiscountPercetangeSpinEdit_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
{
    decimal price, percentage, calculatedPrice;

    price = (decimal)supplierPriceSpinEdit.EditValue;
    percentage = Convert.ToDecimal(e.NewValue, CultureInfo.InvariantCulture);

    calculatedPrice = Popust.Izracunaj(price, percentage);

    productBindingSource.SupplierPriceWithDiscount = calculatedPrice;

}

Upvotes: 1

Related Questions