Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6475617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:43:32+00:00 2026-05-25T06:43:32+00:00

I’m writing a WindowsForms application in MS Visual C#, and I need two forms

  • 0

I’m writing a WindowsForms application in MS Visual C#, and I need two forms to be able to reference each other. As I test, I created two buttons on Form1 — one button that displays Form2 and another button that hides it (code is below).

I want to do the same thing for Form2 — create two buttons that hide or show Form1. I used the same method that I used for Form1, but when I compile the application, it seems to get caught in an infinite loop and I get a StackOverflow message.

How can I change the code so that both forms are able to reference each other?

Form1 code:

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    Form2 frm2;
    public Form1()
    {
        InitializeComponent();
        frm2 = new Form2();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        frm2.Visible = false;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        frm2.Visible = true;
    }
}
}

Form2 code:

namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
    Form1 frm1;
    public Form2()
    {
        InitializeComponent();
        frm1 = new Form1();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        frm1.Visible = false;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        frm1.Visible = true;
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:43:33+00:00Added an answer on May 25, 2026 at 6:43 am

    Forms2’s code should be

    namespace WindowsFormsApplication1
    {
    public partial class Form2 : Form
    {
        Form1 frm1;
        public Form2(Form1 parent)
        {
            InitializeComponent();
            frm1 = parent;
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            frm1.Visible = false;
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
            frm1.Visible = true;
        }
    }
    }
    

    Even though the both talk to each other, one must be created first and passed to the second one.

    Form1 will need to be tweeked to

    public Form1()
    {
        InitializeComponent();
        frm2 = new Form2(this);
    }
    

    The other way to do it is create both and pass it after construction

    namespace WindowsFormsApplication1
    {
    public class SomewhereElse
    {
        public void SomeFunction()
        {
             Form1 form1= new Form1();
             Form2 form2= new Form2();
    
             form1.frm2 = form2;
             form2.frm1 = form1;
        }
    }
    
    public partial class Form2 : Form
    {
        public Form1 frm1 {get; set;}
        public Form2(Form1 parent)
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            frm1.Visible = false;
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
            frm1.Visible = true;
        }
    }
    
    public partial class Form1 : Form
    {
        public Form2 frm2 {get; set;}
        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            frm2.Visible = false;
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
            frm2.Visible = true;
        }
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I am writing an app with both english and french support. The app requests
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.