[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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
3
Medium Priority
?
501 Views
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.
0
Comment
Question by:e_livesay
3 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 35444023
"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?...
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosed.aspx

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.
0
 
LVL 1

Expert Comment

by:ulfur
ID: 35445695
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()
{
	InitializeComponent();
	formX = new FormX();
	// repeat for FormY and Z
}
private void ButtonBClicked(object sender, EventArgs e)
{
	formX.Show(this);
	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.Close();
		}
		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.
0
 

Author Closing Comment

by:e_livesay
ID: 35462189
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.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

868 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