Click here to Skip to main content
15,899,126 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My application is like this:
Form1 = startup form . Reads some data from database and when finishes it send the data through parameters to form2:
Form2 f2=new Form2(param1, param2);
and saves all the data it receives from parameters to some variables
(Form2=> has 2 constructors : one with parameters and one whithout)

Form 2 open. Click on a link=> Form2 hides and form 3 opens.
Click on a link on form3 , and I want to go to the instance of Form2 that was hidden (and gots all the variables set)

How can I do this? Because I really need to keep both constructors for Form2
Posted
Comments
Pheonyx 5-Apr-13 7:54am    
What have you tried? do you have any code snippets?
Luci C 5-Apr-13 7:58am    
I don't have any code... I read some threads in forums...but I think all apply in the case I have one contructor whithout parameters... Maybe, if there is another method to send data between forms, I could succeed

private MyDialogForm _FormInstance;

public void ShowOptions()
{
if (_FormInstance == null)
{
_FormInstance = new MyDialogForm();
_FormInstance.FormClosing += (s,e) =>
{
e.Cancel = true;
_FormInstance.Hide();
}
}
_FormInstance.Show();
}

Exactly how you do this will depend on whether you want form3 visible once Form2 is re-shown. If not, then your easiest solution is to use ShowDialog:
Form2
C#
Form3 f3 = new Form3();
Hide();
Form3.ShowDialog();
Show();

If it is, then the best thing to do is to have Form3 signal an event to Form2 that tells it it should show itself again.

Handing data between the forms isn't difficult. These should help you:

Transferring information between two forms, Part 1: Parent to Child[^]

Transferring information between two forms, Part 2: Child to Parent[^]

The other option is to do all this in Form1 and have it do the work:
Transferring information between two forms, Part 3: Child to Child[^]
 
Share this answer
 
public partial class Form2 : Form
{
public Form2()
{

}
public Form2(string par1,string par2)
{

}


public void LinkButton_Click(object sender, EventArgs e)
{
Form3 objFrm3 = new Form3(this);
objFrm3.Show();
}
}

public partial class Form3 : Form
{
Form2 objFrm2 = null;
public Form3(Form2 obj)
{
objFrm2 = obj;
}

public void LinkButton_Click(object sender, EventArgs e)
{
// you will now get all your varialbes in form2
objFrm2.Show();
}
}
 
Share this answer
 
Comments
Luci C 5-Apr-13 8:07am    
Thank you, it worked
only one question:
you passed all the variables between form2 and form3 as objects, right?
prince7827 5-Apr-13 9:11am    
i am passing entire form2 object in form3 constructor and using the same object to again open it. so it will preserve your values in form2 object.
If you use
Form2 obj = new Form2();
obj.Show();
then it won't work for you.
OriginalGriff 5-Apr-13 10:07am    
Reason for my vote of one: that is a very poor idea. It locks the design of the two forms together and ensures that neither can be changed without considering the effects on the other.
This is a bad practice as it is against the principles of OOP.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900