Kerala Microsoft Users Group

How to Open one form from Another Form - Simple Windows Form Question.

Latest post 03-31-2009 4:54 AM by Anuraj. 5 replies.
  • 03-30-2009 11:09 AM

    How to Open one form from Another Form - Simple Windows Form Question.

    Hi All

    I am facing a basic problem with C# windows forms.

    In my application, I have a login form and the Main form, on successful login, I need to close the Login form and then need to show the Main Form. How can I do that?

    FrmLogin

    Login_Click
    //So process

    frmMain mainform = new frmMain();
    mainform.Show();
    this.Close();

    I think while calling this.Close(), it is disposing all the objects created in the class. How can I resolve this? I know I can use this.Hide(), but don't seems a good solution. I think I can do the same in VB.Net without any problem.

    Please help.

    Thanks

    Anuraj P
    http://www.dotnetthoughts.net

    THIS POSTING IS PROVIDED "AS IS" WITH NO WARRANTIES, AND CONFERS NO RIGHTS.
    BEWARE OF BUGS IN THE ABOVE CODE; I HAVE ONLY PROVED IT CORRECT, NOT TRIED IT.

    • Post Points: 25
  • 03-30-2009 11:20 AM In reply to

    Re: How to Open one form from Another Form - Simple Windows Form Question.

    MDI?

    Did not get your correct problem anyway, make the main form the startup object and show login form (as dialog). So that you will have control over both forms without issues.

     

    Praveen V Nair, PMP
    Tech Blog | PM Blog

    • Post Points: 25
  • 03-30-2009 1:19 PM In reply to

    Re: How to Open one form from Another Form - Simple Windows Form Question.

    Sorry. Let me explain it. I am developing a C# Winform application. It is not a MDI application, it is a SDI, somw wizard like stuff. The first screen contains a Media Player control, and playing a Movie from net. So the first form loading taking some time. And I need to add a Login for this application. So I wrote the code like this.

    Login

     

    void SignIn_Click(Object o, EventArgs e)
    {
    //Code for Authentication
    if(IsAuthenticated())
    {
    MainForm MainUI = new MainForm();
    MainUI.Show();
    this.Close();
    }
    }

    And the Program static class, I wrote like

    static void Main()
    {
    Application.EnableVisualStyles();
    Application.Run(new Login());
    }

    But when ever I am calling this.Close() the whole application is ending. I found some work around like Hiding the Login form and in the Main Window closing, I am closing the owner window too.

    Like this

    void SignIn_Click(Object o, EventArgs e)
    {
    //Code for Authentication
    if(IsAuthenticated())
    {
    MainForm MainUI = new MainForm();
    MainUI.Show(this);
    this.Hide();
    }
    }

    And in MainForm_FormClosing event

    if(this.OwnerForm != null)
    {
    this.OwnerForm.Dispose()


    It is working fine. But I think it is not the right way of doing it. Is there any other option available? 

    Thanks

    Anuraj P
    http://www.dotnetthoughts.net

    THIS POSTING IS PROVIDED "AS IS" WITH NO WARRANTIES, AND CONFERS NO RIGHTS.
    BEWARE OF BUGS IN THE ABOVE CODE; I HAVE ONLY PROVED IT CORRECT, NOT TRIED IT.

    • Post Points: 5
  • 03-30-2009 1:31 PM In reply to

    Re: How to Open one form from Another Form - Simple Windows Form Question.

    I found one more work around

    I created one more class, which actually lauching the application, which inherits from ApplicationContext and in Program class, I am creating the instance of that class. And using the ShowDialog() and Dialogresult I am loading the second form

     public class Launcher:ApplicationContext

        {

            public Launcher()

            {

                Form1 frmMain = new Form1();

                Form2 frmLogin = new Form2();

                if (frmLogin.ShowDialog() == DialogResult.Yes)

                {

                    frmMain.Show();

                }

                else

                {

                    this.Dispose();

                }

            }

        }

    But only problem I am facing right now, is I need to call Application.Exit() method, in each Windows Form closing event.

    Still I don't think it is a good way.

    Thanks

    Anuraj P
    http://www.dotnetthoughts.net

    THIS POSTING IS PROVIDED "AS IS" WITH NO WARRANTIES, AND CONFERS NO RIGHTS.
    BEWARE OF BUGS IN THE ABOVE CODE; I HAVE ONLY PROVED IT CORRECT, NOT TRIED IT.

    • Post Points: 25
  • 03-31-2009 4:49 AM In reply to

    Re: How to Open one form from Another Form - Simple Windows Form Question.

    Hello Anuraj,

    Instead of calling the Login form contructor from Main, how about calling the MainForm itself?

     

    static void Main()
    {
    Application.EnableVisualStyles();
    Application.Run(new MainForm());
    }

     

    Then as part of the load event of MainForm you check the login. It should solve your issues..Right?

    Regards

    Sreejumon

    http://sreesharp.com/

    • Post Points: 25
  • 03-31-2009 4:54 AM In reply to

    Re: How to Open one form from Another Form - Simple Windows Form Question.

    Reply |Contact |Answer

    Thanks Sreejumon for the reply. Yes it will solve issue. But my Main Form contains a Video from a URL and it is taking some time. And on that showing the login form, creating some jerking and flickering in the Media player. So I dropped that idea. Now I am Hiding the Login form on successful login and while exiting from the main application, I am closing the login window. It is working now. Smile

    Thanks

    Anuraj P
    http://www.dotnetthoughts.net

    THIS POSTING IS PROVIDED "AS IS" WITH NO WARRANTIES, AND CONFERS NO RIGHTS.
    BEWARE OF BUGS IN THE ABOVE CODE; I HAVE ONLY PROVED IT CORRECT, NOT TRIED IT.

    • Post Points: 5
Page 1 of 1 (6 items) | RSS