473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How get a reference to another form?

Form B needs to call a public method of Form A. How can form B get a
reference to the instance of Form A using its name or any other
method? Is there a collection of form instances that can be searched?
--
Bill
Nov 16 '05 #1
12 11936
You'll need to declare and instantiate a new instance of A and reference it
from there
//Within formB
formA f = new formA();
f.SomeMethod();
"Bill Todd" <no@no.com> wrote in message
news:le********************************@4ax.com...
Form B needs to call a public method of Form A. How can form B get a
reference to the instance of Form A using its name or any other
method? Is there a collection of form instances that can be searched?
--
Bill

Nov 16 '05 #2
Creating a new Instance will not work. I need access to the existing
instance of Form A.

I assume from your answer that the .NET framework does not maintain a
collection of form instances.

I am accustomed to an environment where referring to one form instance
form another is easy and I am struggling to understand how forms are
supposed to communicate with each other in the .NET environment.

Right now the only method I can see to do this is to have the
application's main form be responsible for creating every other
modeless form in the application. When the main form creates a new
form instance it does so by calling a static method that takes a
reference to the main form as a parameter and returns a reference to
the new form. That way all forms have a reference to the main form and
the main form has a reference to all other forms.

Is there a better way to handle this?

Just one example is the case of a database applicaiton. You cannot
write a database application where every form has its own connection
to the database. If forms are going to share a connection object they
have to have a reference to the form that contains the connection
object.

Thanks for any insight into this.
--
Bill
Nov 16 '05 #3
If one form creates the other form, pass a reference to the first form to
the constructor of the second form... i.e. formA is the first form and it
creates formB with:

FormB formB = new FormB(this);

and create a constructor for FormB as:

Public FormB(FormA formA)
{
}

HTH,

Dale

"Bill Todd" <no@no.com> wrote in message
news:js********************************@4ax.com...
Creating a new Instance will not work. I need access to the existing
instance of Form A.

I assume from your answer that the .NET framework does not maintain a
collection of form instances.

I am accustomed to an environment where referring to one form instance
form another is easy and I am struggling to understand how forms are
supposed to communicate with each other in the .NET environment.

Right now the only method I can see to do this is to have the
application's main form be responsible for creating every other
modeless form in the application. When the main form creates a new
form instance it does so by calling a static method that takes a
reference to the main form as a parameter and returns a reference to
the new form. That way all forms have a reference to the main form and
the main form has a reference to all other forms.

Is there a better way to handle this?

Just one example is the case of a database applicaiton. You cannot
write a database application where every form has its own connection
to the database. If forms are going to share a connection object they
have to have a reference to the form that contains the connection
object.

Thanks for any insight into this.
--
Bill

Nov 16 '05 #4
How about the case where FormA creates FormB then FormB creates FormC
and FormC needs to call a method of the existing instance of FormA?

Bill

On Mon, 3 May 2004 21:31:11 -0500, "DalePres" <no****@nomail.com>
wrote:
If one form creates the other form, pass a reference to the first form to
the constructor of the second form... i.e. formA is the first form and it
creates formB with:

FormB formB = new FormB(this);

and create a constructor for FormB as:

Public FormB(FormA formA)
{
}

HTH,

Dale

"Bill Todd" <no@no.com> wrote in message
news:js********************************@4ax.com.. .
Creating a new Instance will not work. I need access to the existing
instance of Form A.

I assume from your answer that the .NET framework does not maintain a
collection of form instances.

I am accustomed to an environment where referring to one form instance
form another is easy and I am struggling to understand how forms are
supposed to communicate with each other in the .NET environment.

Right now the only method I can see to do this is to have the
application's main form be responsible for creating every other
modeless form in the application. When the main form creates a new
form instance it does so by calling a static method that takes a
reference to the main form as a parameter and returns a reference to
the new form. That way all forms have a reference to the main form and
the main form has a reference to all other forms.

Is there a better way to handle this?

Just one example is the case of a database applicaiton. You cannot
write a database application where every form has its own connection
to the database. If forms are going to share a connection object they
have to have a reference to the form that contains the connection
object.

Thanks for any insight into this.
--
Bill


--
Bill
Nov 16 '05 #5
Bill Todd <no@no.com> wrote:
How about the case where FormA creates FormB then FormB creates FormC
and FormC needs to call a method of the existing instance of FormA?


Then when FormB creates FormC, it should pass the FormA reference which
was passed to it to FormC as well - or make it available as a property.

Forms are just objects, like any other. Think about how you'd solve the
problem if you weren't dealing with forms, and apply it to forms.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Jax
Surely form C should accept form B in the constructor, then as form B already has form A you have all three
I personally prefer to use delegates to do this, so as to keep my methods private, it's a cleaner method but is limited as you need a delegate for each method (so it's best if you just have one refresh method, or your just hiding the form and showing it, in which case you just send Show to the next construcotr in the form of a delegate)

jax
Nov 16 '05 #7
Jax <an*******@discussions.microsoft.com> wrote:
Surely form C should accept form B in the constructor,
then as form B already has form A you have all three.
Well, it depends on the situation. Sometimes form C isn't necessarily
logically linked to form B, but *is* logically linked to form A - in
which case there's no need for form C to know anything about form B at
all.
I personally prefer to use delegates to do this, so as to keep my
methods private, it's a cleaner method but is limited as you need a
delegate for each method (so it's best if you just have one refresh
method, or your just hiding the form and showing it, in which case
you just send Show to the next construcotr in the form of a
delegate).


Yes, it certainly depends on what you're actually doing with the other
forms.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
Jax
True, I was just considering it in progressional terms, if there are to be many forms it could be a clearer method otherwise it may end up being very confusing knowing which form references which
But still, we're just arguing semantics here, I apologise

ja

Nov 16 '05 #9
Thanks. I think I understand what you are saying, although I have no
idea how to do it. I need to do more reading about delegates.

However, using a delegate seems like a lot of extra work for no
benefit. What is the benefit of keeping a method private when you want
to be able to call that method from other object instances?

Sorry if I am being dense.
--
Bill
Nov 16 '05 #10
Thanks. That answers my question.
--
Bill
Nov 16 '05 #11
Jax
Well for example if your programming in teams and you dont other people to accidently use your methods thats where private can come in handy, it's also just a general object orientated convention to attempt to keep most of your methods private

Creating a delegate is very simple and it makes things simpler in the long run because you may modify FormA in some way it might mean you have to make changes to FormB which accepts A (it's happened to me before!). So it ulitmately keeps the two seperate which is sometimes desirable

Say if I wanted to do my usual hide delegate so one form can hide and the FormB thats spawned carries a delegate to the show method of FormA

first declare the delegate somewhere in the namespace (or elsewhere if you like

public delegate void ShowDelegate()

then in formA

FormB fb = new FormB(new ShowDelegate(this.Show))
fb.Show()
this.Hide()

Then in Form

public class FormB : For

private ShowDelegate showDel

public class FormB(ShowDelegate sDel

showDel = sDel
this.Closing += new System.ComponentModel.CancelEventHandler(FormB_Clo sing)

// then you want it to fire when formB closes so
private void FormB_Closing(object sender, System.ComponentModel.CancelEventArgs e

showDel();

Now when FormB closes, FormA will magically re-appear
If anything it's a good idea to learn to use delegates just because they're very useful things

I Hope that was vaguely interesting

jax
Nov 16 '05 #12
Thanks. Very helpful.
--
Bill
Nov 16 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Vanessa | last post by:
Hi, I would like to know whether there's any way for me to pass an object by reference to another form? Regards Vanessa
2
by: Zippy | last post by:
Some months ago, we requested help from this newsgroup on how to replace the library reference of a database with another library reference, prior to creating an MDE. I got the following answer...
3
by: Lyn | last post by:
Hi, I have been experiencing a problem passing a LIKE statement in the WHERE argument of a DoCmd.Openform statement. I have posted that issue separately. However, in an attempt to work around...
14
by: simonmarkjones | last post by:
Hi, I'm having a bit of trouble editing an old database that was created quite a while ago by someone else. There is a form that lets the user select a member of staff and show details about the...
4
by: Vaughn | last post by:
When I pass a reference to my current form, frm_x, when I create and show another form, frm_y, do I have access to all of the public methods, controls, members, etc.. of frm_x from frm_y? In my...
2
by: Suzanne | last post by:
Hi all, I'm reposting this message as I'm experiencing this problem more and more frequently : I really hope someone out there can help me as I've been tearing my hair out on this one for a...
1
by: Nicolas | last post by:
I got an exe which include a form MDI ===> FORM A I got another dll wich include a form =====> FORM B In my FORM A I can launch FORM B and assign FORM B.MdiParent = FORM A But from FORM B I...
5
by: Chris | last post by:
How do I reference a control on another page? Regards.
7
by: planetthoughtful | last post by:
Hi All, I have an app that presents a small main form when run. When a particular button is clicked, I'd like to briefly display another small form directly below the main form, regardless of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.