473,394 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to update the parent form on closing one of it's child forms?

I have a parent form that launches several child forms. Upon closing the child form I need to update several controls on the parent form. I'm guessing that I do this by using an Event of some type, but I'm fairly new to C# and not sure how to accomplish this. Thanks for your help.
Nov 30 '10 #1
2 14574
GaryTexmo
1,501 Expert 1GB
You are correct, you can use events here. When you create your child form inside your parent form, simply attach to the child form's FormClosing or FormClosed event. As an example...

Expand|Select|Wrap|Line Numbers
  1. ChildForm childForm = new ChildForm();
  2. childForm.FormClosed += new FormClosedEventHandler(childForm_FormClosed);
  3. childForm.Show();
... you can then create the childForm_FormClosed method and do whatever you like in it. Alternatively, this is a good time for you to learn about delegates :)

Create a delegate on your child form and call it ParentUpdateDelegate (or something like that). Next, create a public property, perhaps called UpdateParent, on the child form that allows access to that delegate and provides get and set functionality. In your form closing or form closed event, whichever you prefer, you can call the delegate with the following code...

Expand|Select|Wrap|Line Numbers
  1. if (UpdateParent != null)
  2.   UpdateParent();
It's important to do the null check because it's quite possible the programmer may not have set the delegate, leaving it null when it gets called.

In your main form, when you create the child window, you simply assign the property you created to a method with a matching signature to your delegate. You can name this method anything you like.

As an example, I created a child form object, ChildForm, with an UpdateParentDelegate delegate and an UpdateParent property. I put the call to UpdateParent in the form closed event. In my main form I have a button. This button, when clicked, creates a new instance of ChildForm, assigns its UpdateParent property to a method called UpdateMe, disables the button, then shows the form. In the UpdateMe method, I simply re-enable the button.

The result is when I click the button it opens the child form and disables the button. When the child form closes it will make a call to the delegate, which points to the UpdateMe method on the parent form. The UpdateMe method runs and the button is enabled again.

Both methods work just fine. The advantage of a delegate is that you can send parameters along, which will let you pass data that your parent may need. If you do the event way you can still get the data, it just needs to be available via public properties. In both cases you'll need to make sure you get the data before the child form disposes itself.

I hope that helps you!
Nov 30 '10 #2
You can do so using Show and Close method of the Form. Let us suppose that
you have 3 forms, Form1 (the main form), Form2 and Form3.

From Form2, if you want to open Form3 and close Form2, you can use the
following code:

Form3 f3 = new Form3();
f3.Show(); // Opens the Form3

this.Close(); // Closes the current form (Form2)

If you close Form1 (which might be the main form), the entire application
will close.

--
Nand Kishore Gupta
"Jason Huang" wrote:
May 2 '19 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: jinu | last post by:
Hello, My application loads with a form which is the mdi parent and a menu item click (main menu of the parent form) brings up a child form. I have to wait for a considerable amout of time to...
2
by: Brindley | last post by:
Hi there, In my Mdi application, I have placed a set of controls on the parent form to call child forms. The problem is that the controls hide the child forms. I have used SendToBack when Im...
1
by: tdmailbox | last post by:
I have added search buttons to some access fomms. I have a seach button in both the parent and the child form. My issue is that when I search for a last name in the last_name field of the...
8
by: al | last post by:
Greetings, If I instansiate child forms in MDI parent, what is the way to close them. I can't find an event related to child to do such task. I want to close 3 child forms, all in the same...
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
1
by: andreas | last post by:
Hi, I have a parent FormA and two different child forms. In one childform there is a tabcontrol with textboxen and i want to fill these textboxen in this active childform with a statement in the...
4
by: LCAdeveloper | last post by:
Help! Another newbie question I'm afraid. I have a toolbar on an MDI form, which I can control OK to produce a child form. When the child form is active, the appropriate MDI parent form toolbar...
0
by: Scott H. | last post by:
I have an MDI parent form that creates mdi child forms each of which are represented by a tab item. I use a third party control to generate the tabbed MDI child forms. Each tab or MDI child form...
4
by: raj_genius | last post by:
I hav two queries, whc are as follows: FIRSTLY: is it possible to access the controls(by name) of a parent form(MDI) from its child forms??if yes then how??plzz provide a coded example in VB if...
1
by: jiaudheen | last post by:
hi i have a multiline textbox and a button in parent form. in the child form i have text boxes,comboboxes,button. the thing is i click the button in the parent form ,the child form opens. i fill the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.