locked
Changed event RRS feed

  • Question

  • I have a problem with the changed event and I am almost sure I am doing something wrong, but I do not know what. Maybe you can help me.

    In my application I have a OrderDetail table and a Product table (of course the OrderDetail table has a Product field). In the OrderDetail.vb I have the Product_Changed event, so when the user changes the product in OrderDetails I can update some fields in the OrderDetail record. For example when the product field of OrderDetail is changed I update the Price field of OrderDetail with the Price field from the Product table. Is it right? We update OrderDetail.Price field with the value of Product.Price field when the user changes the OrderDetail.Product field.

    To do that I have written the Product_Changed() Event in the OrderDetail class.

    Price = Product.Price

    It seems to work fine, but if I manually change the OrderDetail.Price in runtime (it happens when I sell a product more expensive that its real price), it changes it right and I can even save it to the database. Since here, it is all right.

    What is my problem? When I refresh the screen or when I close and open the window it has again the original price in OrderDetail instaid of the price I manually set. Why? Because when I refresh it call the Product_Changed event. But the product has not been changed... why does it run that event then?

     

    What am I doing wrong? Should I write in another event maybe?

     

    Thank you


    Carlos Adrián Martínez
    Thursday, September 15, 2011 11:21 AM

Answers

  • Yes I also had this problem (exactly the same, Product price). It's odd why Lightswitch executes Changed event everytime, even if the product was not changed.

     

    The solution is like Carlos said but it'll give you error if the OrderDetail is new (because it does  not have an old value)

    Try this :

    If Me.Id = 0 OrElse Me.Details.Properties.Price.OldValue <> Me.Price Then
    Price = Product.Price
    End If 
    

    Hope it helps.


    Like Yann Said : "If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer". This will help people find the answers that they're looking for more quickly."
    • Proposed as answer by Nadjib A. Bait Thursday, September 15, 2011 2:00 PM
    • Marked as answer by Carlos Adrián Thursday, September 15, 2011 2:17 PM
    Thursday, September 15, 2011 2:00 PM

All replies

  • I've had this same problem myself when I've wanted to calculate a username, but also want to be able to manually override it ocassionally.

    What you'll need to do is add an "PriceOverriden" property to your Product, which you can then set if you manually change the price. But don't set it in the Price_Changed event, or you'll have the same problem that you have now. Each property's Changed event gets fired when the entity is loaded (if you think about it, that makes sense, & *usually* it's what you want). But as you've discovered, it makes overriding a property that's being set by another property a bit difficult.

    Set the PriceOverriden in the screen, in something like the TextChanged event for the Price control (this *should* only fire when the user changes the value, otherwise you may have to use the LostFocus event). Do you know how to implement the TextChanged/LostFocus event?

    It's a little bit of "mucking around", but it's a also little bit of an unusual situation (meaning it doesn't happen all that often). The LS team has tried to cater for the "most common" scenarios. LS *can* handle "uncommon" situations, like this, but it just requires a little extra coding.


    Yann

    (plus ça change, plus c'est la même chose!)

    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer".

    This will help people find the answers that they're looking for more quickly.

    Thursday, September 15, 2011 11:44 AM
    Moderator
  • Hi Carlos

    Here's my suggestion: in the field price of the entity in the Create method type: Price u003d 0.

    And change: Price = Product.Price to:

    If Price = 0 then
    
                Price = Product.Price
    
    End if
    

    In this way only if the price is zero, ie when the record is created, is assigned the value of the price.


    Jaime. If you found this post helpful, please: VoteAsHelpful. If it answered your question, remember to: MarkAsAnswer.
    Thursday, September 15, 2011 12:47 PM
  • If I do in that way, it would not update when I really change the product.
    Carlos Adrián Martínez
    Thursday, September 15, 2011 1:29 PM
  • Could it work something like it?

    if Product.OldValue <> Product.NewValue then

    Price =Product.Price

    End if

     

    What I want is to know if the user has really changed the product field or not, but I do not know how.

     


    Carlos Adrián Martínez
    Thursday, September 15, 2011 1:44 PM
  • Absolutely correct, it would *not* change when you wanted it to.

    Yann

    (plus ça change, plus c'est la même chose!)

    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer".

    This will help people find the answers that they're looking for more quickly.

    Thursday, September 15, 2011 1:57 PM
    Moderator
  • Yes I also had this problem (exactly the same, Product price). It's odd why Lightswitch executes Changed event everytime, even if the product was not changed.

     

    The solution is like Carlos said but it'll give you error if the OrderDetail is new (because it does  not have an old value)

    Try this :

    If Me.Id = 0 OrElse Me.Details.Properties.Price.OldValue <> Me.Price Then
    Price = Product.Price
    End If 
    

    Hope it helps.


    Like Yann Said : "If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer". This will help people find the answers that they're looking for more quickly."
    • Proposed as answer by Nadjib A. Bait Thursday, September 15, 2011 2:00 PM
    • Marked as answer by Carlos Adrián Thursday, September 15, 2011 2:17 PM
    Thursday, September 15, 2011 2:00 PM
  • It's midnight here now, so maybe I'm not thinking straight, but isn't "Product.OldValue <> Product.NewValue" the *same* as Product_Changed?

    Yann

    (plus ça change, plus c'est la même chose!)

    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer".

    This will help people find the answers that they're looking for more quickly.

    Thursday, September 15, 2011 2:13 PM
    Moderator
  • It should be... but it is not. Because Product_Changed triggers when you refresh even where there is not any product change. When we way "Product.OldValue<>Product.NewValue" what we mean is "Has it really changed?", or are you just refreshing?

    It is solved and works fine.

     

    Thank you all


    Carlos Adrián Martínez
    Thursday, September 15, 2011 2:17 PM
  • It means it has really changed! I think it's a bug in LS.

     

    @Yann : Yes it seems that Changed event is always fired, even if the property didn't change ! I've got lot of troubles with that. 


    Like Yann Said : "If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer". This will help people find the answers that they're looking for more quickly."
    Thursday, September 15, 2011 2:27 PM
  • Hi all,

    I have posted a similar question here and also with a workaround. The workaround suggested in this thread works as long as the user only makes a single change before saving, but not if he does multiple changes.

    I have also posted this as a bug here (please vote) and done a bit of research. As far as I can tell, this problem occurs only on projects started with the Beta 2 version of LightSwitch. Can the rest of you confirm or deny that assumption? If you start a fresh project against the same database, does the same problem occur again?

    All the best,
    Rune

    • Proposed as answer by andrew_mezoni Thursday, November 3, 2011 2:07 PM
    • Unproposed as answer by andrew_mezoni Thursday, November 3, 2011 2:07 PM
    Thursday, November 3, 2011 6:44 AM
  • Hi Nadjib,

    I had a similar problem. Your solution works like a charm. Thanks for your post.

    Lars

    Friday, February 3, 2012 10:08 AM
  • Having same issue with VS 11.  So not fixed yet.  Best workaround I could find that seems to address all senerios is

    partial void Product_Changed()
    {
        // Needed because of a bug in LS. On screen load/refresh, Property change fires like user changed product.
        if (this.Details.EntityState == EntityState.Unchanged || this.Product==null)
            return;
        // Else, entity was really changed, so update the unit price from the product data.
        this.UnitPrice = this.Product.ListPrice;
    }
    When screen is loaded, the first test will return and not change line item (which is good).  Only if user actually changes product, will the price update as expected.
    Sunday, May 27, 2012 3:37 AM
  • William,

    You are a GENIUS!   :)

    Cheers!

    Thursday, May 31, 2012 10:21 PM