Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hey i have a login form so when i click login so that it goes to another form thats working my problem is that i want to have the login form close as soon as the other form opens. ive tried close(); but then it closes both windows,and how to get the 2nd winform fullscreen , tnx in advance
Posted
Updated 15-Jul-15 23:35pm
v2

Basically there are at least three possibilities
- login is the startup form, not the other one. Change from project settings
- the login form owns the other for and because of this the other form is closed when login is closed. Check if you have defined owner somehwere in the code.
- during the login close you execute such code that closes the other form

To maximize the form use WindowState[^] property
 
Share this answer
 
v2
If your login form is the startup form from your project, then when it closes, your application does as well - so any other forms it opens will also be closed.
Probably the best way to handle this is pretty simple - when your user correctly enters the login details:
C#
MainForm mf = new MainForm();
Hide();
mf.ShowDialog();
Close();
This way, the code will stop until the mail form is closed, but the login form will no longer be visible. The login form (and your app) will close when your main form does.

"okay uhm il try that code now, see cos in the 2nd form ill be having a switch user button which re opens the login form"


That's also easy to handle:
You can set a return value from the main form and inspect that in the login form, either via the DialogResult or via a "NewUser" bool in your main form.
For example:
C#
MainForm mf = new MainForm();
Hide();
mf.ShowDialog();
if (mf.NewUser)
   {
   Show();
   }
else
   {
   Close();
   }

Using the DialogResult is the same:
C#
MainForm mf = new MainForm();
Hide();
if (mf.ShowDialog() == DialogResult.OK)
   {
   Show();
   }
else
   {
   Close();
   }

And you set the "Switch User" button to have the DialogResult of OK and it'll close the form and return OK to your login form.

"now it doesnt re open the login form -_- ugh ima noob"


OK - go back a step.
This is the code in your "SwitchUser" button handler, yes?
The simplest way to do this is to do it manually to start with.
If you want to show the login form again, then set the main form DialogResult:
C#
private void SwitchUserButton_Click(object sender, EventArgs e)
    {
    if (MessageBox.Show("Are You Sure To Exit ?", "Exit", MessageBoxButtons.YesNo) == DialogResult.Yes)
        {
        DialogResult = DialogResult.OK;
        }
    }
The main form will close if you press "yes" and it will return OK.
If you press no, it won't close or return anything.
If you close the main form in any other way, it will return "Cancel"
(You can make the button do it automatically by setting the properties, but then you have to "cancel" it if the user says "No")
 
Share this answer
 
v3
Comments
jamesmc1535 16-Jul-15 5:56am    
okay uhm il try that code now, see cos in the 2nd form ill be having a switch user button which re opens the login form
OriginalGriff 16-Jul-15 6:10am    
Answer updated
jamesmc1535 16-Jul-15 6:20am    
tnx man \m/ very helpfull
jamesmc1535 16-Jul-15 6:36am    
lol sorry for the string of question , uhm i added a message box "are you sure /yes no" if i click yes it closes and opens the login form , but when i click no it also closes and opens login
MessageBox.Show("Are you sure", "Logout/switch user", MessageBoxButtons.YesNo, MessageBoxIcon.Question); theres the code how do i add the no to function
OriginalGriff 16-Jul-15 6:38am    
Show the actual code you used, with some lines either side for context.

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