StackoverflowException on passing value to another form c# - c#

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?

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();

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.

Related

How to close a form if a form is already opened in c#

I am trying to open a single form at once. If user tries to open another form while one form is already open it should not open it.
my code :
else if (instrument_name == "Micrometer")
{
this.Hide();
Form2 f2 = new Form2();
f2.ShowDialog();
this.readclose()
}
here only if this form is not opened already then form 2 is opened.
the solution could be :
Form fc = Application.OpenForms["UpdateWindow"];
if (fc != null)
fc.Close();
fm.Show();
but I don't know where should I write this because if I put this Form1_Load then obviously it will not contain null even for the first time.
Should I put this in program file?
You can create smth like 'form manager', which will monitor all opened forms.
In situation when form is close the manager opens it, otherwise reopen it.
This solution is similar to your, but in this case all logic is concentrated in one place and easy to understand or maintenance it.
You can do it different ways. But one of it is to implement a singletone pattern. For that you have to modify your Form2 the following way:
public partial class Form2 : Form
{
private static Form2 Instance;
private Form2()
{
InitializeComponent();
}
public static Form2 GetForm2Instance()
{
if(Instance==null) Instance = new Form2();
return Instance;
}
public static void CloseOldForm2AndOpenNewForm2()
{
if (Instance != null) Instance.Close();
Instance = new Form2();
Instance.Show();
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
Instance = null;
}
}
As you can see the Form2() is private. In order to load form you have to use the following code:
//Case 1 if you porefer to use existing instance of the Form2
Form2 myForm = Form2.GetForm2Instance();
myForm.Show();
//Case 2 if you prefer to close old one and create a new Form2
Form2.CloseOldForm2AndOpenNewForm2()

Communicating between two Forms in C# [duplicate]

This question already has answers here:
Communicate between two windows forms in C#
(12 answers)
Closed 5 years ago.
I have the following:
I have a GUI with two forms. Form2 is opened via Form1 by a button. Both forms have Textboxes and I want them to communicate with each other (one form can grab the entries of textboxes from another form). What I did now in Form2 is:
private Form1 m_form = null;
public Form2(Form1 f)
{
InitializeComponent();
m_form = f;
}
and for the Textboxes functions like:
public String getLocation()
{
return LocationBox.Text;
}
That works fine. So Form2 can read the entries from Form1. Now I wanted Form1 to read the textbox entries from Form2 and tried the same thing (which is probably wrong):
private Form2 m_form2 = null;
public Form1(Form2 f2)
{
InitializeComponent();
m_form2 = f2;
}
and then some functions like the one I've posted but everytime I want to read a textbox with Form1 which is in Form2 I get "null" and "NullReference" Exception. Where is the error?
EDIT: Ok I solved a part. Adding
Form2 m_form2 = new Form2(this);
m_form2.Show();
solves the problem with the NullReferenceException. Without the line
m_form2.Show()
it passes empty strings but now everytime I hit a button the form2 appears.
I think the problem is that you never call the consturctor public Form1(Form2 f2) . When you open Form2 from the first one you have to save that instance within your instance of Form1:
void createForm2() {
Form2 frm2 = new Form2(this);
this.m_form2 = frm2;
}
try this in Form1:
this.TextBoxName.Text = m_Form2.getLocation();
or in Form2:
m_form.TextBoxName.Text = this.getLocation();
Note
in Form1 when call form 2:
m_Form2 = new Form2(this);
//do anything with your code
For sending values between two forms, you may
1-> Send the values in the constructor of the second form. You may create a paramterized constructor and send the values when you initialize the form as :
Form1 obj = new Form1(Object);
2-> You may take a reference in to your first form in the second form.
In second form,
public Form1 objForm1;
and in First Form,
Form2 objForm2=new Form2();
Form2.objForm1=this;
and then you can use Form2's objForm1 to refer to Form1's textbox or any control.
Consider you want to send all values from Form1 to Form2
In your second form you must have a variable of type Form1 that refers to the prev form. So in second form,
public Form1 objForm1;
and then you need to send the current instance of the Form1 to Form2 as
Form2 objForm2=new Form2();
Form2.objForm1=this;
i.e. the objForm1 that you created in Form2 refers to this instance of Form1.
Now in Form2 you may use any of Form1's control or variable as,
Form1.TextBox1 or Form1.Variable

How to modify controls from a different form in C#

Is there a way to modify the text in a textbox, or something like that from a different form?
In my program, I need to spawn a new Form2, and have all the textboxes hold information based on stuff entered in Form3. So far I've only done:
Form2 form2 = new Form2();
form2.Show();
I just don't know how to access form2.textbox1.Text, for example, from form3. I've looked online but didn't find quite what I was looking for, and I've tried a few different things with no success.
Thanks! :)
Pass the instance of Form2 into Form3:
public Form3(Form2 referrer)
{
var txt = referrer.TextBox1Text;
}
and then when calling it:
Form3 f3 = new Form3(this);
f3.Show();
and you'll just have to make sure that you build a property on Form2 like this:
internal string TextBox1Text { get { return textBox1.Text; } }
Assuming that you are instantiating the Form2 within the code behind of Form3 (calling "new" Form2())...
You can create a public method within Form2 that will provide the accessibility you need. Example:
(within Form2 - code behind)
public SetTextboxTextProperty(string text)
{
this.Textbox1.Text = text;
}
From Form3:
Form2 form2 = new Form2();
form2.SetTextboxTextProperty("your data");
form2.Show();
The problem is that the controls defined in each form are not public objects, so you can't access them from outside the form.
Why don't you define a public method in each form to retrieve/set the value you need from the controls you need?
I think that it would be a better approach than exposing your controls.

Show main hidden form from subfrom

i have 2 forms
Form1 the main form
Form2 the subform
When i open form2 i want to hide form1 <- the main
And when i close form2 i want to show form1
I tried
Form1 mform = new Form1();
mform.Show();
But its open new form not the hidden form
Found solution
In form 2
Form Main;
public Form2(Form MainForm)
{
Main = MainForm;
InitializeComponent();
}
in form 1
Form2 SubForm = new Form2(this);
SubForm.Show();
this.Hide();
So i can show the hidden form1 from form2 by Main.Show();
you have to keep a reference to the Main form in the Form2 instead of creating a new From.
in your Form2 you should have a property to keep a reference to the Form1 something like the blow:
public Form MainForm { get; set; }
Just pass the reference of main form to the Form1 and keep it to access it later.
Form1 mform = new Form1(MainForm mform);
mform.Show();
When you open a new subform FORM2, you just have to hide the main form using form1.Hide() and show the new one Form2.Show().
When you close the subform, you just call the form1.Show().
TO implement this, u gotto have reference of main form.
there is no need for keeping the references. Use the following code in the child form
if (this.ParentForm != null)
this.ParentForm.Show();

get back hidden FORM from another FORM

I have two forms Form1 and Form2
I am opening Form2 from Form1 on button_Click
Form2 obj2 = new Form2();
this.Visible = false;
obj2.Show();
then I want to get back Form1 Visible (on disposing Form2) in same states of Controls on which I left.....
Your Form2 doesn't know anything about Form1. It will need a reference to it (you can do that by adding a Form type property on Form2 and assign Form1 to it after construction):
//In Form2
public Form RefToForm1 { get; set;}
//In Form1
Form2 obj2 = new Form2();
obj2.RefToForm1 = this;
this.Visible = false;
obj2.Show();
//In Form2, where you need to show Form1:
this.RefToForm1.Show();
Oded's answer will work perfectly well, another option with the same result will be to expose public event in Form2 called for example "AfterClose", invoke it when Form2 is disposing and have Form1 add event handler where it show itself. Let me know if you're interested and I'll give some sample code.

Categories