How to call the existing form in ASP.Net C# windows form?

  • Thread starter Thread starter Jason Huang
  • Start date Start date
J

Jason Huang

Hi,

Thanks for help in advance!
In Form1, I "New" a Form2 and ShowDialog the Form2, it's OK.
My intention is passing a value from Form2 to Form1.
However, in Form2, I shouldn't "New" a Form1, 'cuz it will open a new Form1,
the new Form1 will get the value. And the original Form1 get nothing!
What do I need to do to let the ORIGINAL Form1 get the target value?
Any help will be highly appreciated.

Jason
 
hello,
You can create a public static property and set it to any value and you can
access it in form1.
eg
public static string myvalue
{
set {value = "what ever"};
get {return value;}
}
 
Hi,

You have several ways to do so, like:

1- Pass a reference from form1 to form2, then when needed you can update
form1 , note that form1 should have a public member to receive the value
back
// in form 1
form2 = new Form2( this);
form2.ShowDialog();

//in form 2:
//constructor
public Form2( Form1 parent) { this.parentForm = parent;}

// in a method
parentForm.PublicProperty = "what ever value";


2- make tha value accesible to form1 as a public property:
// in form 1
form2 = new Form2( this);
form2.ShowDialog();
form1.receivedvalue = form2.PublicProperty;

3- Make a static public property in form1 , this is the last resort,

//form1
public static string A_Property { get.. set }

//form2
//inside a method:
//note I use the Form1 type, not any instnace
Form1.A_Property = "a value" ;


cheers,
 
Thank you for help me!
But I got another question for this.
Saying that Form3 and Form4 will be also using the Form2, then how do we
build up the Form2 constructor,
so that Form3 and Form4 can get the value passed from Form2? Given that
Form1, Form2, Form3 and Form4
have no mdi relationship.
Thanks in advance again.


Jason
 
Hi Jason,


You kind of lost me, too many forms for so early in the morning :)

If your app is not MDI, and you know you will only have one instance of each
form created at the most, you can use a static property in each individual
form to hold a reference.


cheers,
 

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

Back
Top