Ok so I have what is a simple problem in my eyes, but I cannot seem to make it work in any way. I have two forms and get from the first to the second real easy, using a basic show and hide code snippet as follows

GamescreenFrm secondForm = new GamescreenFrm();

         private void TwoplayerBtn_Click(object sender, EventArgs e)
        {
            this.Hide();
            secondForm.Show();

However, using a button to get back to the first form only returns errors no matter how hrd I try to make it work. i have used the same type of code to no avail, along with many other suggested solutions on tutorials and forums all over the internet. Can anyone show me how to get this infernal program to work?

Thanks in advance :)

Recommended Answers

All 11 Replies

The word error is a bit vague. Please specify.

Hmm lets see...

The hide code to hide the second form is fine, it's the show bit there's a problem with.
Putting Firstform.Show simply doesn't work, while trying to create a new object in the second form of the first one like

FirstFrm firstForm = new FirstFrm();

//I have also used other numbers like thirdForm, but for showing it to you I just left it as firstForm, though I am aware it on't work

         private void NewgameBtn_Click(object sender, EventArgs e)
        {
            this.Hide();
            firstForm.Show();
        }

Spits the following message out at me:
"An unhandled exception of type 'System.StackOverflowException' occurred..." and it points to the object definition. I simply want to get back to the first form from the second, I don't know why I'm finding this so difficult :/

I'm not sure, but I think you derived the two forms from the same object. Hence the stack overflow in trying to show and hide.

Ok, so how can I fix this to get me back to my first form? I'm sorry if it seems like a really dumb question, I just haven't coded in C# before.

There are no dumb questions, only dumb answers.
I should add a new form class item to your project.

Ok so how do I do that to make it respond to my show control? As is, my thing says:

public GamescreenFrm()
        {
            InitializeComponent();
        }

        public class Form : MainFrm
        {
           
        }

and then later just the simple show command:

private void NewgameBtn_Click(object sender, EventArgs e)
        {
            this.Hide();
            MainFrm.Show();
        }

Check this code

//MainForm
//your code 
SecondForm object1=new SecondForm();
this.Hide();
object1.Show();
//rest of code 


//SecondForm
//your code 
this.Close();
//rest of code
void SecondForm_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
{
MainForm object2=new MainForm();
object2.Show();
}

Hmm I think this is a lot closer to working, but the first screen refuses to come up again. Thanks a lot for all of your help guys, I wont need this in a few hours but should I leave the thread unmarked so it can be solved if I cant make it work?

Following code is working absolutely fine i have checked it

// code in first form
 private void Form1_Load(object sender, EventArgs e)
        {
           
        }
     

        private void buttonOn1stForm_Click(object sender, EventArgs e)
        {
             SecondForm object1=new SecondForm();
            this.Hide();
            object1.Show();
        }
//
//code in 2nd form
 private void buttonOn2ndForm_Click(object sender, EventArgs e)
        {
        this.Close();
        //rest of code
        }
        void SecondForm_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            Form1 obj1 = new Form1();
            obj1.Show();
        }

System.StackOverflowException .This exception occurs when the object is created inside constructor of the same class

What you have to do, is to pass a class variable reference of Form1 to Form2. And then use this variable to show form1 again, like this:

//on form1:
Form2 f2;
//on method to show Form2:
if(f2 == null)
    f2 = new Form2(this);
f2.Show();
this.Hide(); //DO NOT close Form1 - because its the main form, and cannot be closed! otherwise the whole application will be closed!!

//
on form2:
Form1 f1;

public Form2(Form1 _f1)
{
     //Form2 constructor
     InitializeComponent();
     this.f1 = _f1;
}

//in method to close Form2 and show back Form1:
//best event it to use Form_Clocing();
f1.Show();
this.Close();  //or this.Hide();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.