Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. closing second form without closing first form

closing second form without closing first form

Scheduled Pinned Locked Moved C#
helpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    alfie max15
    wrote on last edited by
    #1

    I have two forms. there is a button in the first form, when clicked will open up the second form. Now what i want is that when i click the button on the first form again i want the second form to close. i am aware of the code for opening a second form from the primary.

    secondform.show();

    but can any one help me in providing with the code for closing this second form??? the second form should close and the first form should stay open.

    B 1 Reply Last reply
    0
    • A alfie max15

      I have two forms. there is a button in the first form, when clicked will open up the second form. Now what i want is that when i click the button on the first form again i want the second form to close. i am aware of the code for opening a second form from the primary.

      secondform.show();

      but can any one help me in providing with the code for closing this second form??? the second form should close and the first form should stay open.

      B Offline
      B Offline
      BillWoodruff
      wrote on last edited by
      #2

      The simplest possible way to do this is:

      private void btnVisibleControl_Click(object sender, EventArgs e)
      {
      secondForm.Visible = ! secondForm.Visible;
      }

      But, what if the second Form shows a 'CloseBox, and the user closes it ? To guard against that you can define a FormClosing EventHandler in the second Form:

      private void secondForm_FormClosing(object sender, FormClosingEventArgs e)
      {
      this.Hide();
      e.Cancel = true;
      }

      Note that 'secondForm will be closed when your first Form is closed automatically only if the 'secondForm was created and shown in your first Form's code. Automatic closing of Forms created in the "Main" Form's code, when the Main Form closes, is a standard behavior of the Windows Form programming model. But, there's a better way, where all the code is put in the main Form:

      private secondForm f2;

      private string strClose = "Close Second Form";

      private string strOpen = "Open Second Form";

      private void FormTemplate_Load(object sender, EventArgs e)
      {
      f2 = new secondForm();

      // by assigning 'secondForm's Closing EventHandler here:
      // we can synchronize the Button Text easily since the
      // the EventHandler will have access to the Button
      f2.FormClosing += f2\_FormClosing;
      
      btnVisibleControl.Text = strOpen;
      

      }

      // prevent the instance of 'secondForm from actually closing
      private void f2_FormClosing(object sender, FormClosingEventArgs e)
      {
      f2.Hide();

      // update the Button Text
      btnVisibleControl.Text = strOpen;
      
      e.Cancel = true;
      

      }

      // change the Button Text so the end-user sees
      // what pressing the Button will do
      private void button1_Click(object sender, EventArgs e)
      {
      if (f2.Visible)
      {
      btnVisibleControl.Text = strOpen;
      }
      else
      {
      btnVisibleControl.Text = strClose;
      }

      // or you can use this to make the code (above) simpler:
      // btnVisibleControl.Text = (f2.Visible) ? strOpen : strClose;
      
      f2.Visible = ! f2.Visible;
      

      }

      Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview

      A 1 Reply Last reply
      0
      • B BillWoodruff

        The simplest possible way to do this is:

        private void btnVisibleControl_Click(object sender, EventArgs e)
        {
        secondForm.Visible = ! secondForm.Visible;
        }

        But, what if the second Form shows a 'CloseBox, and the user closes it ? To guard against that you can define a FormClosing EventHandler in the second Form:

        private void secondForm_FormClosing(object sender, FormClosingEventArgs e)
        {
        this.Hide();
        e.Cancel = true;
        }

        Note that 'secondForm will be closed when your first Form is closed automatically only if the 'secondForm was created and shown in your first Form's code. Automatic closing of Forms created in the "Main" Form's code, when the Main Form closes, is a standard behavior of the Windows Form programming model. But, there's a better way, where all the code is put in the main Form:

        private secondForm f2;

        private string strClose = "Close Second Form";

        private string strOpen = "Open Second Form";

        private void FormTemplate_Load(object sender, EventArgs e)
        {
        f2 = new secondForm();

        // by assigning 'secondForm's Closing EventHandler here:
        // we can synchronize the Button Text easily since the
        // the EventHandler will have access to the Button
        f2.FormClosing += f2\_FormClosing;
        
        btnVisibleControl.Text = strOpen;
        

        }

        // prevent the instance of 'secondForm from actually closing
        private void f2_FormClosing(object sender, FormClosingEventArgs e)
        {
        f2.Hide();

        // update the Button Text
        btnVisibleControl.Text = strOpen;
        
        e.Cancel = true;
        

        }

        // change the Button Text so the end-user sees
        // what pressing the Button will do
        private void button1_Click(object sender, EventArgs e)
        {
        if (f2.Visible)
        {
        btnVisibleControl.Text = strOpen;
        }
        else
        {
        btnVisibleControl.Text = strClose;
        }

        // or you can use this to make the code (above) simpler:
        // btnVisibleControl.Text = (f2.Visible) ? strOpen : strClose;
        
        f2.Visible = ! f2.Visible;
        

        }

        Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview

        A Offline
        A Offline
        alfie max15
        wrote on last edited by
        #3

        thanx for the reply...

        secondform.close();

        this code worked

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups