Windows UI problem if user adds new child and cancels before save.Windows UI problem if user adds new child and cancels before save.
Old forum URL: forums.lhotka.net/forums/t/2452.aspx
Tommybouy posted on Thursday, March 01, 2007
From the reading I have been doing it appears that child collections and the methods within child objects are designed to handle when the collection is databound to a grid. Specifically for adding and deleting new records. The question I have is, when a user chooses to add a new child object we open an editable form using the ShowDialog method and depending upon whether the user Saves or Cancels would determine whether the object would get added to the collection. If we use the AddNew method the object is already added to the collection and even if the user chooses to cancel the add, we would then need to go thru the collection and remove the new object. Is this correct?
I have seen use of the Assign method where an ID is passed to add an object to a collection but in that case the object being added already exists in the database.
Does someone have an example of how they add a child to a collection via code where a CancelEdit on the child form would not add it to the collection?
Thanks
Tom
xal replied on Thursday, March 01, 2007
In cases where I needed such functionality, I opted to have a way to create a child without adding it to the collection. You can either add a public factory method on the child or you can add a public (non-shared) function to the collection that returns a new child. I usually use the 2nd option.
So, it would be like this (very pseudocode):
Dim collection as ListObject = ListObject.GetListObject()
==============
Dim child as ChildObject
child = collection.CreateNew()
EditChild(child)
============
Public Sub EditChild(ByVal child as ChildObject)
If ShowEditForm(child) = DialogResult.OK Then
child.applyedit()
If Not collection.Contains(child) Then
collection.Add(child)
End If
Else
child.CancelEdit
End If
End Sub
I use something like that edit child method to handle editing of existing items and adding of new items in scenarios like yours.
Andrés
Jimbo replied on Saturday, March 03, 2007
You might want to review Rocky's comments in post 12807 etc regarding
new child objects... which basically says that Andres method is
possibly not the best way to do it. That was my approach before I
needed to refer to the parent in the child dialog and Rocky
provided the reasons why..
Copyright (c) Marimer LLC