Connecting Tech Pros Worldwide Forums | Help | Site Map

Access public methods in multple copies of the same form

Mark Chimes
Guest
 
Posts: n/a
#1: Jan 26 '07
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




--------------------------------------------------------------------------------
I am using the free version of SPAMfighter for private users.
It has removed 1840 spam emails to date.
Paying users do not have this message in their emails.
Try SPAMfighter for free now!



Marc Gravell
Guest
 
Posts: n/a
#2: Jan 26 '07

re: Access public methods in multple copies of the same form


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


Mark Chimes
Guest
 
Posts: n/a
#3: Jan 26 '07

re: Access public methods in multple copies of the same form


Hi Marc,

I am using...
string someVariable = Application.OpenForms[i].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



"Marc Gravell" <marc.gravell@gmail.comwrote in message
news:%23%23rMJvRQHHA.3812@TK2MSFTNGP06.phx.gbl...
Quote:
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
>
--------------------------------------------------------------------------------
I am using the free version of SPAMfighter for private users.
It has removed 1841 spam emails to date.
Paying users do not have this message in their emails.
Try SPAMfighter for free now!


Bruce Wood
Guest
 
Posts: n/a
#4: Jan 27 '07

re: Access public methods in multple copies of the same form




On Jan 26, 3:30 pm, "Mark Chimes" <markchi...@gmail.comwrote:
Quote:
Hi Marc,
>
I am using...
string someVariable = Application.OpenForms[i].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; }
}
Quote:
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[i] 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.

Mark Chimes
Guest
 
Posts: n/a
#5: Jan 27 '07

re: Access public methods in multple copies of the same form


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" <brucewood@canada.comwrote in message
news:1169857395.752592.315210@m58g2000cwm.googlegr oups.com...
Quote:
>
>
On Jan 26, 3:30 pm, "Mark Chimes" <markchi...@gmail.comwrote:
Quote:
>Hi Marc,
>>
>I am using...
>string someVariable =
>Application.OpenForms[i].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; }
}
>
Quote:
>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[i] 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.
>
--------------------------------------------------------------------------------
I am using the free version of SPAMfighter for private users.
It has removed 1841 spam emails to date.
Paying users do not have this message in their emails.
Try SPAMfighter for free now!


Bruce Wood
Guest
 
Posts: n/a
#6: Jan 27 '07

re: Access public methods in multple copies of the same form




On Jan 26, 9:26 pm, "Mark Chimes" <markchi...@gmail.comwrote:
Quote:
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 ...
}

Mark Chimes
Guest
 
Posts: n/a
#7: Jan 28 '07

re: Access public methods in multple copies of the same form


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



--------------------------------------------------------------------------------
I am using the free version of SPAMfighter for private users.
It has removed 1841 spam emails to date.
Paying users do not have this message in their emails.
Try SPAMfighter for free now!


Bruce Wood
Guest
 
Posts: n/a
#8: Jan 28 '07

re: Access public methods in multple copies of the same form




On Jan 28, 5:56 am, "Mark Chimes" <markchi...@gmail.comwrote:
Quote:
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.

Closed Thread