Communicating between forms...

  • Thread starter Thread starter AMeador
  • Start date Start date
A

AMeador

I have a form where a user will check boxes next to items that have
been completed. They will click a button on the form to mark the items
as completed. When they do this, I will have the 'parent' form
instantiate another form where the details of this completed event will
be entered. Once the data is entered on this 'child' form, the data
entered should be updated/added to the database. The 'child' form also
has 'Cancel' and 'Cancel All' buttons. When the user hits 'Cancel',
the 'child' child form will not update/add the data and it will close.
If multiple items have been marked as complete in the 'parent' form,
the parent form will then open a new 'child' form for the details of
the next completed item, and so on, and so on. However, if the 'Cancel
All' button is hit, I want the process to stop. What is the best way to
make this work? How will the data from clicking 'Cancel All' get back
to the 'parent' form? I'm referring to the forms as 'Parent' and
'Child' as they are NOT MDI forms. I am a relative newbie to this stuff
so please be gentle in your answer ;) Thanks!
 
I think the simplest is to have a variable with the form that processed the
data update, something like:

bool bCommitData = false;

Initially false until the entire process goes through all child forms and
the last form processes the OK button, in which case bCommitData = true. If
it is true then update database.

However this will only work if the parent of all forms has the database
update code in it. You will just want to update the entire database if the
process of going through all forms is completed. Then at the very end, send
all updates to the database.
 
I was planning to put the data update code in the 'child' form.
These items are not dependant on each other, so if the process is
stopped in the middle, that would be fine. But even in your example
above, I still don't know the best way to go about letting the 'parent'
form know that a child has recived a 'Cancel All' or, if doing it as
you suggested, how to return the data collected in the child to the
parent. Would this be done by the use of some global array of objects?
Or, should I have the 'child' form fire some kind of an event at the
'parent' level before it closes down? If something like this was done,
how would you do that? Or, is there a better way that I'm not thinking
of? My code background is not very large at this point in relation to
object oriented and event driven approaches.
 
You can implement an attribute on your child form called bCancelAll for
example, initially set to false. Then using the C# attribute featue you can
write something like this in your child forms:

public bool IsCancelAll {
get
{
return bCancelAll;
}
}

also assign mrCancel value to both Cancel and Cancel All buttons.

in your main (parent) form something like this:

ChildForm1 form1 = new ChildForm1(this);
bool x = false, y = false, z = false;

DialogResult formResult = form1.ShowModal();
if (formResult == DialogResult.Cancel) {
if (form1.IsCancelAll) {
// do something because Cancel All was pressed
}
} else {
// OK button pressed
x = form1.CKBox1.Checked;
y = form1.CKBox2.Checked;
z = form1.CKBox3.Checked;
}

This is just a generic example so you can see the structure.
Does that give you any ideas?
 
I think this may be what I'm looking for. Using the exampe you gave, I
will have form1 close if its cancel button is clicked. It seems like
from the way you did your code, that when the form closes, it will
return a parameter, like some kind of closing status, which is stored
in the formResult object. I will look into the DialogResult
class/object to understand it a bit better, but I think this will do
it. Thanks! If it still gives me trouble, I'll get back to you, if you
don't mind. Thanks again.
 
Thanks for the reply - but that went right over my head. Its a bit over
my knowledge level right now. I have read about these things and
kind-of get it but the lingo in that post it tying too many of those
upper level concept around and I can't put it together. Hopefully that
will change soon ;)
 
Back
Top