We help IT Professionals succeed at work.

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

e_livesay asked
Medium Priority
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.
Watch Question

High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Unlock this solution and get a sample of our free trial.
(No credit card required)

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.


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.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.