How can I access a public function in an open usercontrol from another form (WinForms)

Posted on 2009-04-22
Medium Priority
Last Modified: 2012-05-06
I have a usercontrol (uc_Main) of which an instance is loaded and functions as a "main" page. On this control are a dozen or so lables that show total values of invoices, Accounts Receivable and so on.

These labels are updated when the control loads through a public function called "updateStats".

From this control a button opens a modal form (frmFIN_InvoiceClient.cs) that allows me to create invoices. When invoices are being created, I would need to update the totals on uc_Main to reflect the new activities.

I have tried to do it this way:

on frmFIN_InvoiceClient:

private void menuItem6_Click(object sender, EventArgs e)
... create invoices first ...
... then try and run the public function on uc_Main ...
uc_Main uc_Main = new uc_Main();
Which doesn't produce the results I am looking for.

How can I run the updateStats() public function on uc_Main that is currently open?
Question by:conrad2010
LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 24209968
Pass in the owner of the dialog when you show the invoice form:

    frmFIN_InvoiceClient invoice = new frmFIN_InvoiceClient();
    invoice.ShowDialog(this); // <--- passing in the "owner" form to the dialog

Then, in the invoice dialog, you can cast the owner property to the main form type:

    frmMain main = (frmMain)this.Owner;
    // ...now do something with "main"...

Obviously you'll have to change "frmMain" to the type of your main FORM.  Then you'll need to make sure that the Modifiers Property for your UserControl is set to PUBLIC so that you can access it from outside your form.

Another option would be to make the invoice form raise a custom "update" event that the main form subscribes too.  Then simply update the form whenever the event is received...
LVL 30

Expert Comment

ID: 24210090
First, I'd say that syntax like:

uc_Main uc_Main = new uc_Main();

is quite horrible and may lead to confusion. I'd neve give a name to an instance same as to the class. Better would be something like_
uc_Main mainControl = new uc_Main();

Anyway, to update the existing control you need to refer to the existing control, not to create a new one.
How to do:

I'd create a special constrictor for the frmFIN_InvoiceClient form, something like :

public frmFIN_InvoiceClient(uc_Main parentControl)
    _parentControl = parentControl; // set the field


So, now your invoice create form knows about the control to be updated.

The rest should be easy:

private void menuItem6_Click(object sender, EventArgs e)
... create invoices first ...
... then try and run the public function on uc_Main ...


Another issue is  - how to pass required info to the uc_Main instance (parentControl). One way is to pass as arguments in the updateStats method. Another way - you call updateStats() and your uc_Main  "interrogates" invoice form to get the information. Probably you have done this already.

Pls ask questions if the idea is not clear

Author Comment

ID: 24210182
Hi anarki_jimbel,

I had already accepted previous answer before your posting showed (otherwise I would have split the points).

Thanks for your posting...

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Loops Section Overview

850 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