Access public methods in multple copies of the same form

  • Thread starter Thread starter Mark Chimes
  • Start date Start date
M

Mark Chimes

Hi All,

I have multiple copies of the same form open in an app. (These are not MDI
child forms).
I then open a summary form that displays data from each of the previously
open forms.
I have a number of public methods inside the child forms. How do I iterate
thru each child form and run the public method in each?

cheers,
Mark Chimes
 
When you create and destroy the forms, keep track of them in a
collection somewhere (perhaps with a WeakReference so that you don't
accidentally keep them alive if the form is otherwise dead). There is
also the Application.OpenForms property (which you could enumerate,
testing each to see if it is of the correct tye), but I prefer to keep
strick track myself - this allows for a range of tricks (improved
separation, no constant testing, indexing and type checking all come
to mind).

Marc
 
Hi Marc,

I am using...
string someVariable = Application.OpenForms.Controls[j].Controls[k].Text

to gain access to the contents of controls in these forms.

How do I run a public method of this form? For example, I have a pubic
method called ChangeMap(string strState) that I need to call from the main
form.

cheers,
Mark
 
Hi Marc,

I am using...
string someVariable = Application.OpenForms.Controls[j].Controls[k].Text

to gain access to the contents of controls in these forms.


Ugh. That's the nasty way to do it. Much nicer to make properties in
your Form class that return meaningful values and know where to look
them up. For example:

public string CustomerName
{
get { return this.CustomerNameTextBox.Text; }
}
How do I run a public method of this form? For example, I have a pubic
method called ChangeMap(string strState) that I need to call from the main
form.

Well, if you're using the OpenForms list, and you're looking for forms
of class MyForm3, you could do this:

MyForm3 f3 = Application.OpenForms as MyForm3;
if (f3 != null)
{
string customerName = f3.CustomerName;
f3.ChangeMap("....");
}

The "as" operation and the "if (f3 != null)" allow for other types of
forms being open, but you want only the MyForm3 type forms.
 
Bruce, thank you.

This is the kind of info I've been looking for. :-)

One other question (for now).
When my app runs, it auto-opens an MDIChild (WebForm) that contains a couple
of JPG maps on individual WebBrowser controls. These maps arew "hot-spotted"
by using HTML code. When the user clicks on a section of the map, a MyForm
opens with data for that section.
I have multiple copies of MyForm open (between 2 - 20).
I also have a SummaryFrm open (called from the MainApp menu) that grabs a
couple of variables from each open MyForm and displays and then calculates
and displays summaries.
So far so good.

I need SummaryFrm to iterate thru the open MyForms and run a public method
inside MyForm.
I can see this public method easily enough from WebForm, but not from
SummaryFrm. I understand that SummaryFrm has no direct relationship with
MyForm so how do I get the data specific to each MyForm back into
SummaryFrm?

cheers,
Mark

Bruce Wood said:
Hi Marc,

I am using...
string someVariable =
Application.OpenForms.Controls[j].Controls[k].Text

to gain access to the contents of controls in these forms.


Ugh. That's the nasty way to do it. Much nicer to make properties in
your Form class that return meaningful values and know where to look
them up. For example:

public string CustomerName
{
get { return this.CustomerNameTextBox.Text; }
}
How do I run a public method of this form? For example, I have a pubic
method called ChangeMap(string strState) that I need to call from the
main
form.

Well, if you're using the OpenForms list, and you're looking for forms
of class MyForm3, you could do this:

MyForm3 f3 = Application.OpenForms as MyForm3;
if (f3 != null)
{
string customerName = f3.CustomerName;
f3.ChangeMap("....");
}

The "as" operation and the "if (f3 != null)" allow for other types of
forms being open, but you want only the MyForm3 type forms.
 
One other question (for now).
When my app runs, it auto-opens an MDIChild (WebForm) that contains a couple
of JPG maps on individual WebBrowser controls. These maps arew "hot-spotted"
by using HTML code. When the user clicks on a section of the map, a MyForm
opens with data for that section.
I have multiple copies of MyForm open (between 2 - 20).
I also have a SummaryFrm open (called from the MainApp menu) that grabs a
couple of variables from each open MyForm and displays and then calculates
and displays summaries.
So far so good.

I need SummaryFrm to iterate thru the open MyForms and run a public method
inside MyForm.
I can see this public method easily enough from WebForm, but not from
SummaryFrm. I understand that SummaryFrm has no direct relationship with
MyForm so how do I get the data specific to each MyForm back into
SummaryFrm?

Won't the Application.OpenForms thing work for you? I have to admit
that I've never tried it; I've never needed to do that.

Another possibility, as Marc pointed out, is to maintain your own list
in some central place. Here's an example:

Inside WebForm:

private static ArrayList _myForms = new ArrayList();

public static IEnumerable GetMyFormList()
{
return _myForms;
}

public static void AddMyForm(MyForm form)
{
_myForms.Add(form);
}

public static void RemoveMyForm(MyForm form)
{
_myForms.Remove(form);
}

Then, from within MyForm:

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
WebForm.AddMyForm(this);
}

protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
WebForm.RemoveMyForm(this);
}

Then, from anywhere in your application, you should be able to say,

foreach (MyForm f in WebForm.GetMyFormList())
{
... do something ...
}
 
Bruce,

OK, the penny has dropped :-)

Rather than using the Application.OpenForms (which only handles forms opened
from the main app and not child or sub-child forms), I should be building my
own list which allows me to add/delete whenever I need to.

Just one further question :-)
protected override void OnLoad()... how does this method affect myexisting
OnLoad() method?
I realise I could simply take the code and place it inside my OnLoad method,
but I would rather understand what the "override" does.

cheers,
Mark
 
Bruce,

OK, the penny has dropped :-)

Rather than using the Application.OpenForms (which only handles forms opened
from the main app and not child or sub-child forms), I should be building my
own list which allows me to add/delete whenever I need to.

Just one further question :-)
protected override void OnLoad()... how does this method affect myexisting
OnLoad() method?
I realise I could simply take the code and place it inside my OnLoad method,
but I would rather understand what the "override" does.

If you have already overridden OnLoad, then just add the code in
there. If you already have an OnLoad method with that same signature,
then it must say "override", because it is replacing the OnLoad method
of the System.Windows.Forms.Form base class. If you create an OnLoad
method with that same signature and you don't say "override" (or
"new") then the compiler will complain.
 
Back
Top