CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2007
    Posts
    405

    Can recall frmLogIn ?

    suppose, I've program includes 2 frmLogIn and frmMain, the syntax recall frmLogIn in frmMain how ? you see my code below:

    in Program.cs file
    Code:
    ...
    Application.Run(new frmLogIn());
    in frmLogIn.cs file
    Code:
    private void CmdLogIn_Click(object sender, EventArgs e)
    {
        ...
        this.Hide();                                 // of frmLogIn
        this.DialogResult = DialogResult.OK; // of frmLogIn
    
        frmMain _frmMain = new frmMain();
        _frmMain.Activate();
        _frmMain.Show();
    }
    in frmMain.cs file
    Code:
        /* // Don't called frmLogIn in this way 
            // If called frmLogIn in this way will be 2 frmLogIn in memory
        frmLogIn _frmLogIn = new frmLogIn();
        _frmLogIn.Activate();                
        _frmLogIn.Show();
        */
        
        // I want recall frmLogIn in here, the syntax recall frmLogIn how ?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Can recall frmLogIn ?

    What do you mean, 'recall'? Are you saying you want to display the login form again?

  3. #3
    Join Date
    Sep 2007
    Posts
    405

    Re: Can recall frmLogIn ?

    Quote Originally Posted by Arjay View Post
    What do you mean, 'recall'? Are you saying you want to display the login form again?
    Yes, I want re_show frmLogIn when frmLogIn hide because the frmLogIn call to Application.Run(new frmLogIn()). can you recall frmLogIn ?

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Can recall frmLogIn ?

    Quote Originally Posted by dongtrien View Post
    Yes, I want re_show frmLogIn when frmLogIn hide because the frmLogIn call to Application.Run(new frmLogIn()). can you recall frmLogIn ?
    Rather than hide/show frmLogIn, start the app with frmMain.

    Code:
    Application.Run(new frmMain());
    Set frmMain to be initially hidden and then call frmLogIn to authenticate. If the user is authenticated, show the main form (or exit the app as appropriate).

    If you need to call frmLogIn later, simply display is as you would when you display any modal form.

  5. #5
    Join Date
    Sep 2007
    Posts
    405

    Re: Can recall frmLogIn ?

    I do not understand you to said, from my example above you how to rewrite recall frmLogin ?

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Can recall frmLogIn ?

    Quote Originally Posted by dongtrien View Post
    I do not understand you to said, from my example above you how to rewrite recall frmLogin ?
    In order to understand what I'm talking about, you need to understand how Application.Run and the form that is passed to it works. With a .net app, the form you pass to Application.Run controls the lifetime of the application. When you close this form, the application (process) shuts down.

    So that is why you can't call Application.Run twice (once with login and again with main).

    What you need to do is start the app with a form that always exists (although it may be hidden sometimes).

    You can do this two ways. The first way is to pass frmLogin to the app, and when the user logs in, hide frmLogin and create and display the frmMain as a modeless form. When you need to show the frmLogin, hide frmMain and show frmLogin.

    The other way to do this is to do this is to start the app with frmMain but initially hidden, and then display frmLogin as a modal form. If the user can't authenticate, you close frmMain (which causes the app to exit). If the user authenticates, you close frmLogin and show frmMain. If you need to show frmLogin you create another instance of it and display it as a modal form.

    Since most of the functionality of your app isn't just logging on (I presume), I prefer the second approach because there are some subtleties when working with modeless forms that you can avoid by going with the second approach.
    Last edited by Arjay; July 2nd, 2014 at 09:33 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured