ListChanged and calculatinig totals

ListChanged and calculatinig totals

Old forum URL: forums.lhotka.net/forums/t/1647.aspx


Wal972 posted on Tuesday, October 31, 2006

I have added the following code yet my Calculating Code is not being called WHY ? I need the calculation to occur when the amount in the child is changed, as well as when items are added or deleted. Plus I need to move this to the Pay parent as well because it need properties from it. Specific code required for the parent object.  HELP :) Thanks

Protected Overrides Sub OnListChanged(ByVal e As System.ComponentModel.ListChangedEventArgs)

If e.ListChangedType = ComponentModel.ListChangedType.ItemAdded Then

AddHandler Item(e.NewIndex).PropertyChanged, AddressOf OnPayChanged

ElseIf e.ListChangedType = ComponentModel.ListChangedType.ItemChanged Then

AddHandler Item(e.NewIndex).PropertyChanged, AddressOf OnPayChanged

ElseIf e.ListChangedType = ComponentModel.ListChangedType.ItemDeleted Then

OnPayChanged(Me, New System.ComponentModel.PropertyChangedEventArgs("PayDetails-ListChanged"))

End If

MyBase.OnListChanged(e)

End Sub

Protected Sub OnPayChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs)

Calculating code

End Sub

Not sure if this is correct please advise

Bayu replied on Tuesday, October 31, 2006

Starting a new thread on the same subject is close to bashing, and usually does not help much.

I just read your original thread (http://forums.lhotka.net/forums/thread/8349.aspx). It seems like several people already offered advice, but you need more specific code-level help in order to be able to make it work.

In your snippet you add event handlers every time objects are added, changed and removed. This does not make sense to me, I'd say you would add event handlers when the object is added and remove them again when the object is deleted. In your code you keep adding event handlers at infinity, are you aware of what exactly you programmed here? Are you aware that you are just registering an event handler, not actually invoking it?

I can't tell you why your OnPayChanged routine never hits, perhaps your PropertyChanged event is never raised.

In order to know what went wrong, you should first know what you are trying to do.

Bayu

Wal972 replied on Tuesday, October 31, 2006

I added a new thread because the old one was getting long and I needed help, if you think its bashing that was not the intent.

If you can offer actual suggestions I would greatly appreciate it. I am simply following previous  suggestions. This is not an area I have had experience with, so please be patient.

Brian Criswell replied on Tuesday, October 31, 2006

You need to do some research into when you are supposed to add handlers.  Now if I understood correctly, you need a total in the root object to update whenever the amount of an item changes.  This should do that.  You will need to change variable names and such to match your variable names.

In your root object, which has the list as a child:

DataPortal_Create
    'Create root object and child list
    AddHandler _list, AddressOf _list_ListChanged
End Function

DataPortal_Fetch
    'Load root object and child list
    AddHandler _list, AddressOf _list_ListChanged
End Function

Private Sub _list_ListChanged(ByVal sender As Object, ByVal e As ListChangedEventArgs)
    If e.ListChangedType = ListChangedType.ItemChanged Then
       If e.Property.Name = "Amount" Then
          'Calculating code
       End If
    End If
End Sub

malloc1024 replied on Tuesday, October 31, 2006

Keep it simple.  Just loop through the line-items and calculate the total.  The time spent calculating the total is insignificant in most situations. 

 

I would also suggest reading up on events.  There are many websites that are very helpful.  You wouldn’t learn as much if someone gave you the exact code you need.  Most people in this forum are very helpful and are willing to point you in the right direction but you need to take some responsibility and take it from there.


BTW, Bayu was trying to help you.  It is somewhat disrespectful to suggest that his response wasn’t an actual suggestion.  You shouldn’t open another thread with the same question because it casues confusion.  You are also less likely to get the help you need if you create two threads with the same question.

Wal972 replied on Tuesday, October 31, 2006

Thanks Guys, the reason I need exact code was I had done research and was unable to find samples from which to learn. I had done quite a few google searchs using the keywords of the suggestions but unable to find anything. And if I was disrespectful it was unintentional. I had been getting a little frustrated with this problem over the last week and may have taken it out on you guys, so apologies. This forum is great because of the help. My lack of understanding came out the wrong way.

A person can tell to use the flint to start a fire, but sometimes you need to see how its used. That was my situation.

All the references I had found had said to used the listchanged etc, but not the how.

So thanks guys for the help

Wal972 replied on Wednesday, November 01, 2006

THANKS HEAPS EVERYONE

Through your hard work and extreme patience, my code is working exactly how i need it to, and my understanding has increase a hundredfold.

Copyright (c) Marimer LLC