Click here to Skip to main content
15,880,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have two forms,
Form1 - Button click event i have wrote below code.

C#
form2 fm=new form2();
fm.show();

this.visible=false;


Its working fine and as i click on button the secound form is opening and first form is closing.

It this a correct way ???
Please Advice.
thanks !!!
Posted
Updated 16-Feb-22 18:54pm
v2

Your setting the Visible property[^] of the form which is not the same as calling the Close method[^]. If you really want the form to be closed instead of just being invisible do use the Close method.

Regards,

Manfred
 
Share this answer
 
v2
Comments
Sanna_001 22-May-12 9:13am    
Thanks Manfred
I have used closed method but as i clicked button to jump on next form my whole application is getting close. :(
Mohamed Mitwalli 22-May-12 9:16am    
yes because you Close the main Form you should use Hide()
Manfred Rudolf Bihy 22-May-12 9:39am    
So how come you always talk about closing the form when all you want to do is to hide it? You seem a little confused to me. :)
Closing the main form will of course close the application.
Sergey Alexandrovich Kryukov 22-May-12 15:10pm    
My 5, taking into account this note. Most likely, capturing FormClosing to cancel closing of the form and calling Hide instead should work for OP.
--SA
Hi ,
Check this Example

C#
public partial class Form1 : Form
 {
     public Form1()
     {
         InitializeComponent();
     }
     private void Form1_Load(object sender, EventArgs e)
     {
     }

     private void button1_Click(object sender, EventArgs e)
     {
         Form2 frm = new Form2();
         frm.Show();
         this.Hide();
     }
 }


C#
public partial class Form2 : Form
   {
       Form1 frm = new Form1();
       public Form2(Form1 frm2)
       {
           InitializeComponent();
           frm = frm2;
       }
          public Form2()
       {
           InitializeComponent();
       }
       private void Form2_Load(object sender, EventArgs e)
       {

       }
       private void button1_Click(object sender, EventArgs e)
       {
           frm.Show();
           this.Close();

       }
   }

Best Regards
M.Mitwalli
 
Share this answer
 
v2
Comments
Sanna_001 22-May-12 9:28am    
Thanks to all its working fine now.
:)
Mohamed Mitwalli 22-May-12 9:30am    
Your welcome :)
Manfred Rudolf Bihy 22-May-12 9:41am    
Hmm, I think OP is a little confused. Ofcourse closing a main form closes the application, but why does OP keep talking of closing the form when his true intentions are to hide it.
Mohamed Mitwalli 24-May-12 9:37am    
Yes Agree with you .
Prasad_Kulkarni 23-May-12 3:39am    
This is 5 worthy!
this.Hide();
var otherform = new Form2();
otherform.Closed += (s, args) => this.Close();
otherform.Show();

I know its late but hope it helps! :)
 
Share this answer
 
Comments
Dave Kreskowiak 4-Oct-21 11:56am    
Try NINE years late, and you didn't add anything to the discussion that happened way back then.
Sean Ewington 20-Oct-21 16:57pm    
Hi Dave,

Just a reminder it is OK to answer old questions. You are, of course, very welcome to downvote or report solutions that you feel don't anything to the discussion.
Hello all, below code will work perfectly fine, use 'hide()' method to hide the current form and open another form using 'Show()' method on the new forms instance,
public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void Login_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(txtUnm.Text == "Optiq" && txtPwd.Text == "vision_13")
            {
                this.Hide();
                var formConfig = new FormConfiguration();
                formConfig.Show();
            }
            else
            {
                MessageBox.Show("Invalid username and password");
            }
        }
    }
 
Share this answer
 
Comments
CHill60 17-Feb-22 4:20am    
Reason for my downvote: You have added nothing new to this thread and have included irrelevant code which just confuses the solution

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900