1

In my project, there are two forms frmLogin and frmMain. After successful login from frmLogin I am showing the frmMain form to the user by doing something like this:

In frmLogin form button_click event:

frmMain main = new frmMain();
main.Show();
this.Hide();

In frmMain when the user logs out I want to show the same frmLogin form (not the instance). How to do this?

I tried this code: (creating another instance of frmLogin which I don't want)

In frmMain form button_click event:

if (MessageBox.Show("Do you really want to log out?", "Alert", MessageBoxButtons.YesNo).Equals(DialogResult.Yes))
{
    this.FormClosing -= frmMain_FormClosing;
    //
    Process p = new Process();
    p.StartInfo.FileName = Application.ExecutablePath;
    p.Start();
    //
    this.Dispose();
}

I have also tried using internal specifier but no use.

EDIT: As a trainee, I am not allowed to use Static keyword and altering program.cs. If the above approach requires restricted methods (which I have mentioned) then please suggest me an alternate approach.

Mr_Green
  • 40,727
  • 45
  • 159
  • 271

4 Answers4

4

Pass a frmLogin reference to frmMain. Then, just before you dispose of frmMain, show frmLogin.

frmMain main = new frmMain();
main.LoginForm = this;
main.Show();
this.Hide();

Then in the button click event:

if (MessageBox.Show("Do you really want to log out?", "Alert", MessageBoxButtons.YesNo).Equals(DialogResult.Yes))
{
    this.FormClosing -= frmMain_FormClosing;
    LoginForm.Show();
    this.Dispose();
}
Phil Cairns
  • 758
  • 4
  • 15
  • Thank you for response, I think you are missing something related to `LoginForm`, VS is showing red scribbles. – Mr_Green Nov 05 '12 at 05:38
  • Assuming your class names are MyLoginForm and MyMainForm, you'd need definitions in MyMainForm like this: `private MyLoginForm _loginForm; public MyLoginForm LoginForm { get { return _loginForm; } set { _loginForm = value; }}` or something like that (I don't have a code editor open at present. – Phil Cairns Nov 05 '12 at 06:16
  • +1 this is working but unfortunately big code compare to @Sami answer. :) – Mr_Green Nov 05 '12 at 06:30
  • Yeah, I agree. I upvoted Sami's answer because it's cleaner :-) – Phil Cairns Nov 09 '12 at 05:17
3

All what you have to do is assign login page as owner of nextform to be opened

In your login Page call following function where you want to open nextForm

void openNextForm()
{
    Form f2 = new YourForm();    
    f2.owner=this;
    f2.Show();
    this.Hide();
}

In your nextForm (e.g mainForm) write following aginst your button click

void ButtonLogOut_Click(object sender, EventArgs e)
{
     this.Owner.Show();
     this.Hide();
     this.Dispose();
}
Sami
  • 8,168
  • 9
  • 66
  • 99
  • But the point is why would you write so much extra code when you can simple use the `ShowDialog()` method? Doesn't make sense. `ShowDialog()` even allows you to set the parent in the function call itself, without writing extra code. – Abijeet Patro Nov 05 '12 at 06:30
  • @AbijeetPatro I asked a question which was not worthy (which I realized later after seeing your answer), but to make it worthy I gave it some limitations. – Mr_Green Nov 05 '12 at 06:32
  • Yes @AbijeetPatro. You might have seen earlier I had shared this code earlier `Form f2 = new YourForm(); f2.Shown += (s, e) => { f1.Hide(); }; f2.ShowDialog(); f1.Show();` But he had little different query i.e. to show loginform only on logout of nextform and not on close. SO i had to edit – Sami Nov 05 '12 at 06:50
1

I think the cleanest approach when dealing with multiple forms is to create them in Program.cs, and keep all the methods to manage them there, then call those methods from event handlers. Kind of like this:

static class Program
{
    public static MainForm mainForm = new MainForm();
    public static LoginForm loginForm = new LoginForm();

    [STAThread]
    static void Main()
    {
        mainForm.Hide();
        loginForm.Hide();
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(loginForm);
    }

    public static void Login()
    {
        loginForm.Hide();
        mainForm.Show();
        // probably do more here
    }

    public static void Logout()
    {
        if (MessageBox.Show("Do you really want to log out?", "Alert", MessageBoxButtons.YesNo).Equals(DialogResult.Yes)))
        {
            mainForm.Hide();
            loginForm.Show();
            // probably do more here
        } 
    }
}

Then in event handlers you can just call Program.Login() or Program.Logout()

Mike Trusov
  • 1,958
  • 1
  • 15
  • 23
  • thank you for response, this is good but I cant use this. I should have mentioned before, please check my edited question. – Mr_Green Nov 05 '12 at 05:49
  • you should convince them to let you, because creating forms inside forms and then passing references can get really confusing and error prone, after all did they hire you to write good or hacky code? ;) – Mike Trusov Nov 05 '12 at 05:57
1

I don't get why you don't use the ShowDialog() method?

 frmMain main = new frmMain();
 this.Hide();
 main.ShowDialog();
 this.Show();

The login form will be hidden and after the main form is closed, the login form's execution will continue and it will be automatically shown...

Abijeet Patro
  • 2,842
  • 4
  • 37
  • 64
  • yes this is working, good logic but what if I want to use show() instead showDialog(). Just curious about it. – Mr_Green Nov 05 '12 at 06:21