Click here to Skip to main content
15,850,359 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to close current running Form(Form1)while navigating one Form(Form1)to another Form(Form2),after Form navigation(Form2 show) I have to close first Form(form1) with in the Form(Form1).

I tried it but I may not get the solution.
Not like object creations for Forms
In second form
Form1 f1=new Form1();
If I create Object for Form1 in Form2 Constructor,Total form1 is loaded,Performance wise it is not good,With out object creation any other method is there.
and not this.close() method.
Any other solution for this problem

1 solution

Your question is not clear.

1. What does "I tried it but I may not get the solution." mean, exactly? What happened?
2. In your second Form Form2, the lines:
Form1 f1=new Form1();

do exactly what you are telling them to do. The first line creates a new instance of Form1 (it does not reference the original Form1, as you seem to think). The second one simply closes this new instance.

If Form1 is the first form that loads when your application starts then you cannot close it. If you do, then the application also closes because when the first Form closes program flow returns to the method that called it and continues there. This would be fine but the call to Form1 is the last statement so there's nothing else to do and the app closes.

This whole thing would be easier to discuss if you used sensible names for your controls (Forms). For example give them names that represent what they do. Form1 could be renamed to 'MainForm' and Form2 as 'AddressForm', if it was for entering addresses.

However, what I suspect that you are trying to do is have a login form and then a main form, in which case they should be Form1 as 'LoginForm' and Form2 as 'MainForm'. If this is correct then, firstly you should have searched this forum for "login form" before asking (you would have found loads of answers) and secondly you should have described it properly in your question.

If this is a Login Form problem then follow the links in Login form [^].
Share this answer

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