Unloading Forms.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Unloading Forms.

Hybrid View

  1. #1
    Join Date
    Dec 2009
    Posts
    596

    Unloading Forms.

    Hello everyone.

    I have winforms app with a login form in which the user enters a user Id and a password. After the correct user id and password is entered, the main form is displayed. When the main form is closed the Login form is still there. I need to get rid of the login form. I haven't been able to figure out how to do this. How do I get to the the Main Form's closing or unloading event? And when I am able to get to this event what do I type to close the login form?

  2. #2
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,451

    Re: Unloading Forms.

    When do you show the "Login" form.? Post some code please.

    Normally, I would do it like this in the main form Initialization code, or in a controlling class.

    dim fLogin as new frmLogin
    fLogin.ShowDialog();
    isLoggedin = fLogin.WasSuccessfulLogin;
    fLogin.Hide();
    fLogin = null;

  3. #3
    Join Date
    Dec 2009
    Posts
    596

    Re: Unloading Forms.

    Hi Sotoasty.

    I was so messed up that I didn't have any code to show. What I ended up doing was hiding the login form. But that alone wasn't good enouh becuase when I closed the main form Visual Studio had the Blue square (stop debugging) icon lit up. The program was still running. So I added a Form_Closing event by using the lighting bolt and added Application.Exit. So that took care of the running program. It all works now. Thanks.

    in the login form
    Code:
    private void cmdLogin_Click(object sender, EventArgs e)
            {
                if (txtUserID.Text.ToLower() == "guest" && txtPassword.Text.ToLower() == "password")
                {
                    frmMain frmObj = new frmMain();
                    frmObj.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("Login incorrect: UserID = guest and password is password");
                    txtUserID.Text = "";
                    txtPassword.Text = "";
                    txtUserID.Focus();
                }
                   
            }
    in the main form
    Code:
      private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
            {
                Application.Exit();
            }

  4. #4
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,451

    Re: Unloading Forms.

    To me, this is the correct way to do it.


    Startup.zip

  5. #5
    Join Date
    Dec 2009
    Posts
    596

    Re: Unloading Forms.

    I just took a look at that code. That was great. I'll definitly use this approach. Super thank you very much.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center