0

i understand that this below code make a overflow because its called second times. this is the explanation, let say i have form1, form2, form3

And in the form1

Form NewForm2 = new Form2;

And in the form3

Form NewForm3 = new Form2;

When i showing form2 of course its stack overflow because its called second times on Form1 and Form3, Okay, so my question is there a way to connect the Form2 from multiple form called it? Any reference?

3
  • You can pass the NewForm2 reference to the Form3, when you create the 3rd form. Commented Dec 2, 2013 at 11:28
  • stackoverflow.com/questions/2727986/… Commented Dec 2, 2013 at 11:30
  • 3
    Creating 2 separate instances of a form shouldn't cause a SO. Show your actual code. Commented Dec 2, 2013 at 11:31

2 Answers 2

2

so my question is there a way to connect the Form2 from multiple form called it? yes just apply a Singleton Pattern on your form

public partial class Form2 : Form
{

 private static Form2 inst;
 public static Form2  GetForm
 {
   get
    {
     if (inst == null || inst.IsDisposed)
         inst = new Form2();
     return inst;
     }
 }

}

to show your form

  Form2.GetForm.Show();
Sign up to request clarification or add additional context in comments.

2 Comments

available, but i cant passing value to the targeted form (form 2) i mean
i already figured out, using this.Close() on the other form to create the new object thank you...
1

You may use this code to see if Form2 is already created and if so then show it else create new instance of Form2.

var form = Application.OpenForms.OfType<Form2>().FirstOrDefault();

if (form == null)
{
    form = new Form2();
}

form.Show();

but opening 2 forms should not cause SO exception, I assume there is a problem elsewhere but you may try code above and see if it fixed your problems.

1 Comment

yes, the real condition, Form1, is called Form2, Form2 is called Form3 and Form3 is called Form2, this upper answer and you fixed the problem but it cant passing value to targeted form (Form2 i mean)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.