How can a C# UserControl update forms that it opened (i.e., the forms will not be closed and reopened).

Posted on 2011-04-21
Last Modified: 2013-12-16
Hello all,

   I am trying to update forms opened by a UserControl.

   I have a UserControl which has two buttons on it, call them button_A and button_B.
* button_A triggers calculations to be made based on inputs on the UserControl.
* button_B triggers three forms to be opened which allow the user to look at the results of the calculations that have been made by the UserControl.  These forms are opened using the Show method, not the ShowDialog method.
   What I am trying to do is keep the three forms open and updated while the user makes changes to the inputs on the UserControl and pushes button_A to cause new calculations to be made.  That is, I want the user to be able to do the following:
1.) Push button_A to perform calculations.
2.) Push button_B to open the three forms that allow the user to look at the results.  This is the only time the user will ever push button_B in this scenario.
Loop over the following:
3.1.) Modify the inputs on the UserControl.
3.2.) Push button_A to trigger calcuations to be made.
3.3) Automatically update the three forms that were opened via button_B in step 2.  That is, do not push button_B again and do not close and reopen the forms that are already open, just pass them the new results and tell them to present them.
End Loop

   What is a good way to do this?

   I considered using something similar to the OwnedForms property of a System.Windows.Forms.Form but a UserControl does not have an OwnedForms.  I considered making the UserControl maintain its own list of OwnedForms but the problem is in the maintenance.  That is, I don't see an elegant way for the UserControl to find out that a form has
been closed and thus remove it from its list of OwnedForms.
   Another option might be for the UserControl to have an 'I have changed' event and for the forms, when created, to 'listen' for that event.  If they hear it then they can request new data from their parent UserControl (a pointer to which they keep) and display it.  This would avoid the problem of the UserControl having to maintain a list of forms.
   I am developing a C# Windows Application with a target framework of .NET 2.0 using Microsoft Visual Studio 2008.

   Any help would be appreciated.
Question by:e_livesay
    LVL 85

    Accepted Solution

    "I don't see an elegant way for the UserControl to find out that a form has
    been closed and thus remove it from its list of OwnedForms"

    How about you subscribe to the FormClosed() event when you create the forms to be displayed?...

    This will allow you to know when the form has been closed and therefore you can remove it from your list and/or recreate it if necessary.
    LVL 1

    Expert Comment

    To easily update the content of the three forms, make the controls that show the results public or internal. For example, if you use a Label control, set its Modifiers property to Internal.
    Let's say your three forms are FormX, FormY, and FormZ. In your UserControl declare three members:
    private FormX formX = null;
    private FormY formY = null;
    private FormZ formZ = null;

    Open in new window

    Paste the following code in the UserControl:
    public UserControl1()
    	formX = new FormX();
    	// repeat for FormY and Z
    private void ButtonBClicked(object sender, EventArgs e)
    	formX.FormClosing += CancelFormClosing;
    	// repeat for formY and formZ
    	// you need this to be able to close formX, Y, and Z
    	this.ParentForm.FormClosing += ParentForm_FormClosing;
    void ParentForm_FormClosing(object sender, FormClosingEventArgs e)
    	// this will allow normal closing of formX
    	if (formX != null)
    		using (formX)
    			formX.FormClosing -= CancelFormClosing;
    		formX = null;
    	// repeat for formY and formZ
    void CancelFormClosing(object sender, FormClosingEventArgs e)
    	// this will stop the user from closing the form
    	e.Cancel = true;
    private void ButtonAClicked(object sender, EventArgs e)
    	// do the calculations
    	formX.label1.Text = "result";

    Open in new window

    Now, in the designer, attach ButtonAClicked and ButtonBClicked to the respective buttons.

    Hope this helps.

    Author Closing Comment

    Thanks for the help.  My code is now working.  

    I created a variable of type List<Form> named myOwnedForms and added the three forms to that list when they were created.  I also subscribed to the 'FormClosing' event for each of the forms.  When a form is closed, it is removed from myOwnedForms.  Whenever the user performs new calculations, i.e., by pushing button_A, the 'update()' routine of each of the forms in myOwnedForms is called which causes the results of the calculation to be updated.

    A significant advantage of this solution is that the details of how these forms do their 'update' is hidden from the user-control.  As a result, the structure of the code that handles the owned forms stays the same even if the resutls are displayed differently.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Hire Top Freelancers to Complete C# Projects

    Source the talented Expert Exchange community
    for top quality work on your C# projects.

    Hire the best. Collaborate easily. Get quality work.

    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    This video discusses moving either the default database or any database to a new volume.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now