StackOverflowException was unhandled C# - c#

I am getting a odd error that I cant recall ever getting before. I am trying to make a few menus for a small game but somehow something is wrong with my reference to Form1.
Here is the code:
public partial class Form1 : Form
{
Form2 Form2 = new Form2();
Form3 Form3 = new Form3();
public string difficulty = "Makkelijk";
public string guesses = "Normaal";
public Form1()
{
InitializeComponent();
}
private void playButton_Click(object sender, EventArgs e)
{
//Form3.difficulty = difficulty;
//Form3.guesses = guesses;
Form3.Show();
this.Hide();
}
private void optionsButton_Click(object sender, EventArgs e)
{
Form2.Show();
this.Hide();
}
private void exitButton_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
Form2:
public partial class Form2 : Form
{
Form1 Form1 = new Form1();
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Woord toevoeg query
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button3_Click(object sender, EventArgs e)
{
//Form1.difficulty = comboBox1.Text;
//Form1.guesses = comboBox2.Text;
this.Close();
}
}
Is there anything wrong with this?
Thanks in advance.

This is because you are initializing the Form2 inside the Form1 and in Form2 you are initializing Form1, which makes circular initialization and causes to stackoverflow exception.

You are creating a new Form1 in the ctor of Form2 and Form2 in the ctor of Form1.
Each time you create one of those, you create the other as well and so you get into an infinite loop which evantualy fills up your stack.

As said before, circular initialization is causing your exception.
One way of solving it is to make Form2 accept Form1 as constructor parameter.
Form1 form;
public Form2(Form1 form1)
{
form = form1;
InitializeComponent();
}

The first line of Form1 creates a new Form2. The First line of Form2 creates a new Form1. This will keep happening until you run out of memory.
Remove the
Form1 Form1 = new Form1();
from the Form2 definition.

Related

How to show existing form from another form?

i have Form1 and Form2, in Form1 i have some textboxes like username,password and more... and a textbox "region". when user hits "region"(Form1.hide()), then Form2 opens witch has 5 labels with names of regions on it.
so how can i make that when user clicks on a name of region in Form2, Form1 will have the region on it? and keep all the data that the user entered before the region click.
something like this(in form 2):
private void center_Click(object sender, EventArgs e)
{
this.Hide();
Form1.region = "center";
Form1.show();
}
Try creating an instance of Form2 and calling ShowDialog() method to show it
Form2 form2= new Form2();
form2.ShowDialog();
When creating Form2, just pass Form1 as a parameter and edit the textbox value in your click event.
On form1:
private void click_on_region(object sender, EventArgs e)
{
this.Hide();
Form2 frm2 = new Form2(this);
Form2.Show();
}
on form2:
Form1 _frm1;
public Form_Main(Form1 frm)
{
InitializeComponent();
_frm1 = frm;
}
private void center_Click(object sender, EventArgs e)
{
this.Hide();
_frm1.textBox_region.Text = whateverobject.Text;
_frm1.Show();
}
This might not be the prettiest but It'll do for starters.
Form 1 Code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 objForm2 = new Form2();
objForm2.ShowDialog();
textBox_Region.Text = objForm2.RegionName;
}
}
And Form 2 Code
public partial class Form2 : Form
{
public string RegionName
{
get
{
return textBox_Form2_Region.Text.ToString();
}
set { }
}
public Form2()
{
InitializeComponent();
}
}
On Form 2
private void center_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.textbox_region = whateverobject.text;
this.hide();
frm1.show();
}
this will bring up a form1 with the region text on it.

How to pause processing code till the form hides in C#?

I am new to C# and I am using windows forms.
I have form1 and form2, I show and hide form2 from form1 as following:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Form2 frm2 = new Form2();
private void button_showForm2_Click(object sender, EventArgs e)
{
frm2.Show();
//I want to show the following message once form2 hides:
MessageBox.Show("Form2 is hidden. Continue processing next line of code");
}
}
In form2:
private void button_HideForm2_Click(object sender, EventArgs e)
{
Hide();
}
When I run the above code and show form2, form2 shows up with the messageBox at same time. I know it is because when using Show() method it does not hold the program flow and continue executing next lines of code while using ShowDialog() holds the program flow till you close the child form.
What I want to do is ( I do not want to use ShowDialog()) : I want to show form2 and when you finish using it and hide it I want to display the above message (in form1) when form2 hides.
Anyone knows how can I do that? Thank you alot.
I would implement it in this way
Form1:
private void button_showForm2_Click(object sender, EventArgs e)
{
this.frm2 = new Form2(this);
this.frm2.Show();
}
public void ShowMessage()
{
MessageBox.Show("Form2 is hidden. Continue processing next line of code");
}
Form2:
public Form1 _Form1 { get; private set; }
public Form2(Form1 _Form1) { this._Form1 = _Form1; }
private void button_HideForm2_Click(object sender, EventArgs e)
{
Hide();
this._Form1.ShowMessage();
}
Hook the Form's VisibleChanged Event, your code will then get fired when the Forms visibility changes.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Form2 frm2 = new Form2();
private void button_showForm2_Click(object sender, EventArgs e)
{
frm2.VisibleChanged += new EventHandler(this.FormVisibilityChanged);
frm2.Show();
}
private void FormVisibilityChanged(object sender, EventArgs e)
{
frm2.VisibleChanged -= new EventHandler(this.FormVisibilityChanged);
MessageBox.Show("Form2 is hidden. Continue processing next line of code");
}
}

Can't add new nodes to TreeView control

I want to add a node from one form to the other one. I have the following code, but it does not work:
From Form2:
private void button2_Click(object sender, EventArgs e)
{
_HauptFenster = new Form1();
_HauptFenster.AddGroup(textBox1.Text);
this.Close();
}
to Form1:
public void AddGroup(string name)
{
MessageBox.Show(name);
Einträge.Nodes.Add(name);
}
I can see the real name of the node in the MessageBox, but the node is not being added to the TreeView on Form1.
I see two possible scenarios:
You have a Form2 and you need to add a node to Form1's treeview (assuming Form1 has already shown). In that case, you can't use _HauptFenster = new Form1(); which is a new Form1, not the one you already have. What you should do is to make a reference of form1 in form2:
public partial class Form1 : Form
{
Form1 form1;
public Form2()
{
InitializeComponent();
}
public Form2(Form1 form1):this()
{
this.form1= form1;
}
private void button1_Click(object sender, EventArgs e)
{
form1.AddGroup("something");
this.Close();
}
}
and change modify the way you open Form2:
private void button1_Click(object sender, EventArgs e)
{
Form2 form2= new Form2(this);
form2.Show();
}
Form1 has not been shown yet, because you forgot to bring it up:
private void button1_Click(object sender, EventArgs e)
{
Form1 form1= new Form1 ();
form1.Show();
form1.AddGroup("something");
this.Close();
}
I found a solution:
Form1 master = (Form1)Application.OpenForms["Form1"];
master.Einträge.Nodes.Add(name);
I think this is what ShAkKiR said; specifically, scenario 1, where I was trying to add it to a TreeView control in a new instance of Form1, instead of adding to to the existing instance of Form1.

C# - Change title of another Form

How can I change title of Form 1 from my From 2? Here is my code:
Form1:
public void setTitle(string title)
{
this.Text = title;
}
Form2:
private void buttonOk_Click(object sender, EventArgs e)
{
Form1 f1= new Form1();
f1.setTitle(textBoxTitle.Text);
this.Hide();
}
What am I doing wrong?
You should pass Form1 as parameter in Form2's constructor.
Form1 Form_one;
public Form2(Form1 form1):this()
{
Form_one = form1;
}
private void buttonOk_Click(object sender, EventArgs e)
{
Form_one.setTitle(textBoxTitle.Text);
this.Hide();
}
In the method you want to show Form2 you should call like that;
Form2(this).Show();
You should have the actual instance of Form1 which is currently displayed.
While displaying Form1, keep an instance to it in Form2. (I am assuming that you are displaying Form1 from Form2. If not, you should provide that Form1 instance to Form2 while creating an instance of Form2)
public class Form2 : Form
{
private Form1 form1;
private void OpenForm1()
{
form1 = new Form1();
form.Show()
}
}
Then, invoke the setTitle() on that instance:
private void buttonOk_Click(object sender, EventArgs e)
{
form1.setTitle(textBoxTitle.Text);
this.Hide();
}

How to reLoad a form from another form

I have two forms, Form1 and Form2. I use ShowDialog() on Form2 from Form1. How can I run Form1Load() from Form2? Specifically, I want to refresh Form1 from Form2.
Firstly, make sure you assigning the Owner property on Form2 before showing it. This allows you to access the current instance of Form1.
class Form1 : Form
{
public void Method()
{
var form2 = new Form2();
form2.Owner = this;
form2.ShowDialog();
}
}
From Form2 you can this use this.Owner to access the instance of Form1 and call any public methods or access any public properties. (Make sure the load event handler is public)
class Form2 : Form
{
public void Method()
{
this.Owner.form1_Load(null,null); //assuming you don't use these params.
}
}
In your form1_Load() I would recommend putting in a this.Refresh() to "refresh" the form. The refresh doesn't update some things that have data stored, it only repaints the form, so in the load event you will have to manually "refresh" things.
Create an instance of Form1 and then use the Refresh method or your Form1_Load method with that instance.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void ShowMessage(string message)
{
MessageLabel.Text = message;
}
private void ShowForm2(object sender, EventArgs e)
{
Form2 Form2Copy = new Form2(this);
Form2Copy.ShowDialog();
}
}
and
public partial class Form2 : Form
{
Form1 Form1Copy;
public Form2(Form1 Parent)
{
InitializeComponent();
Form1Copy = Parent;
}
public void Button_Click(Object sender, EventArgs e)
{
Form1Copy.ShowMessage("Hello from Form2!");
}
}
Pass in Form1 to the ShowDialog() method of your Form2 instance:
private void Form1_Load(object sender, EventArgs e)
{
this.LoadEventCode();
}
public void LoadEventCode()
{
this.Text = DateTime.Now.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog(this); // <-- pass in Form1
}
Now over in Form2, cast the .Owner property to Form1 and do what you need:
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = (Form1)this.Owner;
f1.LoadEventCode();
}

Categories

Resources