How to pass a form to another form

B

bharathi_dereddy

Hi all,

I have a form1 ,in that i have 2 lable and 2 combo boxes. and a
button. when the user clicks the button,i need to pass form1 to form2.
how i can access the form1 controls in form2.

plz help me
 
M

Marc Gravell

As a general rule, that isn't a great idea as it breaks encapsulation.

One solution is to mark the protection of the controls as "public"
(which you can do in the designer via the properties window, or in
code) but this is not very nice.

A better approach would depend on what you are doing; you could, for
instance, expose some public events and methods on the second form;
this would allow you to interact / respond, without needing to know
about the implementation details.

Marc
 
G

Guest

As Mark indicated, there are elegant and not-so-great ways to do this. One
easy way (not necessarily the best or most elegant) is to pass the first form
into the constructor of the second form, and have a field of type Form in the
second:


class Form Secondform
{
private Form _firstForm;

public Form( Form otherForm)
{
_firstForm = otherForm;
}

-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA): http://www.blogmetafinder.com
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Hi all,

I have a form1 ,in that i have 2 lable and 2 combo boxes. and a
button. when the user clicks the button,i need to pass form1 to form2.
how i can access the form1 controls in form2.

Just create a constructor in form2 that receive a Form1 instance

Remember to also make the controls (or use properties ) to make the controls
available from form2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top