Do I have access to all methods when I pass a reference of a form to another form?

V

Vaughn

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 code, I pass the reference like this:

private void btn_ListEmp_Click(object sender, System.EventArgs e)
{
frm_EmpList frm_empList = new frm_EmpList(this);
frm_empList.MdiParent = this.MdiParent;
frm_empList.Show();
}

---------------------
public Form _frm_emplRec;
public frm_EmpList(DataSet DS_allEmployees, Form frm_emplRec)
{
InitializeComponent();
_frm_emplRec = frm_emplRec;
//_frm_emplRec.txt_ssNum.Text = "MyText"; /* This doesn't compile
although txt_ssNum is public*/
}

How can I pass a reference of the form *and* have control of anything public
inside the form?
Also, is this the best way to pass values from one form to another? If not,
what other option could I use?

Thanks again,
Vaughn
 
E

Egbert Nierop \(MVP for IIS\)

Vaughn said:
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?

Hi

Only to public members. Not to protected or private methods.
 
I

irfan

you could surely be able to get all the methods exposed,
but only if you create an object of the required form
 
V

Vaughn

How would I create an object of the form? I suppose that to do that, I need
more code than this:
public Form _frm_emplRec;
public frm_EmpList(DataSet DS_allEmployees, Form frm_emplRec)
{
InitializeComponent();
_frm_emplRec = frm_emplRec;
}

I assumed that with the code above, I'd be able to access all the controls
of the reference (Eg. _frm_emplRec.txt_ssnum.Text = visible;). But the
controls aren't recognized in the compiler.

Thanks again.
 
G

Gabriel Esparza-Romero [MSFT]

Hi

The problem is that the type of your field '_frm_emplRec' is 'Form'. The
type 'Form' does not have a public member called 'txt_ssnum', that's why
you are getting a compiler error. This can work in other languages that
support late-binding however C# does not.

You need to declared your '_frm_emplRec' field of the same type as the form
that you are getting.

thx
Gabriel Esparza-Romero, Visual C# Team
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 

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