reference a public property in another Form?

N

nick

I have a main form, with a public property. And this form open another form
by click a button. Is it possible that the new created form access the
public property of the main form?

public class MainForm : System.Windows.Forms.Form
{
......
public string P1;
private void mniFreeze_Click(object sender, System.EventArgs e)
{
NewForm fmNewForm= new NewForm();
fmNewForm.Show();
}
.......
}

In the class NewForm, how to access P1?
 
S

Sijin Joseph

You will need to pass a reference of MainForm to NewForm

//Constructor for NewForm
public NewForm(Form mainForm)
{
string P1 = mainForm.P1;
}

private void mniFreeze_Click(object sender, System.EventArgs e)
{
NewForm fmNewForm= new NewForm(this);
fmNewForm.Show();
}



//If you just want to pass P1 to NewForm you can also write the ctor as
public NewForm(string P1)
{
//Do whatever with P1
}

private void mniFreeze_Click(object sender, System.EventArgs e)
{
NewForm fmNewForm= new NewForm(this.P1);
fmNewForm.Show();
}


Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
 

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