Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi everybody,

I have a small problem in windows application in C#.net. I will clearly explain that task in below:
Actually I have two forms i.e, one is login form and another one is home form.

At first the user should enter his credentials after verifying, the second form should be display and at the same time login form has to close.

VB
 'In login form i have created one object for the second form by using
 form2 obj=new form2()
 obj.show()
'this is working fine but i don't want first form so for that i have used,
 form1 obj1=new form1()
 form1.close()

But, while running the application both forms(form1 and form 2) are displaying. I don't want to display form1
so please help me for finding the solution with out using MDI.

Thanks,
Sandeep
Posted
Updated 7-Mar-11 4:40am
v2

You don't want to close Form1: if you do, your application will end, and both form1 and form2 will close.

You need to go back to your course notes, and read up on instances:
form1 obj1=new form1()
form1.close()
There are a number of problems here:
1) obj1 does not refer to the existing form1 you are displaying. When you wrote the new keyword it is supposed to remind you taht it is creating a new instance of form1. You now have two instances: the running one which you can see, and the new one you just created, which you can't.
2) form1.Close doesn't exist as standard. It would be a static method (don't worry about the name, you will find out later) which is not connected to any instance, and so cannot close the current (or indeed any) form.

What you want is this
this is the word you use when you want to explicitly refer to the current instance of a class. So, you could use:
this.Close();
And teh current instance of Form1 would close. Unfortunately, this will also end your application.

Replace your code with:
this.Hide();
form2 f2 = new form2();
f2.ShowDialog();
this.Show();
And you Form1 will appear to close, form2 will appear, and when it closes, for1 will reappear.

OK?
 
Share this answer
 
Comments
#realJSOP 7-Mar-11 15:53pm    
Actually, he would have to add an event handler for the 2nd form in the first one so that it knows the 2nd one has closed and can re-show itself (of course you knew that).
OriginalGriff 8-Mar-11 3:12am    
Not if he used ShowDialog as I suggested: processing waits until the new form is closed. That's why I used it, I didn't want to confuse a beginner with events :laugh:
See if this tip/trick doesn't help:

Multiple Subsequent "Main" Forms in C# Apps[^]

If you want to allow the user to log off, you can simply programatically restart the app, and the first form will be displayed once again.
 
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