Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-server-2005 C# .NET4 , +
I am developing a windows application using c#. I have one form open and on button click of form 1, I am opening form 2, i.e. both Forms form 1 and form 2 are opened. Form2 Show in task-bar property is set to false. I want to close or hide the form 2 on button click event of form 1. I tried to use Hide and Close Method, but the form 2 is still displayed.
Any Help !!!
 
Thanks In Advance !!!
 
bool Display; // declared at class level. 
//btnshow_hide is on form 1.
private void btnshow_hide_Click(object sender, EventArgs e)
        {
            form2 tc = new form2();
            if (Display == true)
            {
                Display = false;                
                tc.Show();
            }
            else if (Display == false)
            {
                Display = true;
                tc.Hide();
                //tc.Close();
            }
        }
Posted 29-Dec-12 1:38am
Ank_ush2.8K
Edited 29-Dec-12 2:37am
v2
Comments
jibesh at 29-Dec-12 7:24am
   
Can you copy the code you did. Solution provided by OriginalGriff should resolve the issue
Ank_ush at 29-Dec-12 7:38am
   
Copied the code, you can review....

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You have to use the Hide or Close method of the form on the actual instance of the form, not on a new one:
 
private Form2 form2;
private void OpenForm2()
   {
   if (form2 == null)
      {
      form2 = new Form2();
      }
   form2.Show();
   }
private void CloseForm2()
   {
   if (form2 != null)
      {
      form2.Close();
      form2 = null;
      }
   }
 

 
"Its not working. I have button on form 1, I want to use the same button to show and hide the form 2."
 
Not exactly a big change:
private Form2 form2;
private void ToggleForm2()
   {
   if (form2 == null)
      {
      form2 = new Form2();
      form2.Show();
      }
   else
      {
      form2.Close();
      form2 = null;
      }
   }
  Permalink  
v2
Comments
Ank_ush at 29-Dec-12 7:00am
   
Its not working. I have button on form 1, I want to use the same button to show and hide the form 2.
Ank_ush at 29-Dec-12 7:39am
   
Review my code....
OriginalGriff at 29-Dec-12 7:49am
   
Yes. Now look at mine. Do you see the difference? That is why my code works, and yours doesn't...
 
If you don't see the difference, it is that I keep a copy of the reference to teh form I open, so I can close it again. You don't.
Ank_ush at 29-Dec-12 8:03am
   
Yes Friend, i changed the code and it worked... Thanks............
OriginalGriff at 29-Dec-12 8:15am
   
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 515
1 Maciej Los 290
2 Richard MacCutchan 265
3 BillWoodruff 235
4 Suraj Sahoo | Coding Passion 175
0 OriginalGriff 8,804
1 Sergey Alexandrovich Kryukov 7,457
2 DamithSL 5,689
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411028.1 | Last Updated 29 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100