none
Windows Forms : Hide and Show

    Question

  • I have two forms 1.Login 2.WorkArea.
    When application will run user will get Login Form, after Login, I am showing WorkArea Form and hiding Login Form.
    On the WorkArea form I have logout button.When user will click on logout I am showing Login form as Dialog. So The workArea form will be idisabled (I dnt want to close it) but when user will again say Login..it shows the second instance of WorkArea opened..
    I want the same instance should be active again...
    How I can do this?

    Test Code :
    Form1

     private void button1_Click(object sender, EventArgs e)
            {
                Form2 _f = new Form2();
                _f.Show();
               this.Hide();
            }
    ---------------------------
    Form2

     private void logoutToolStripMenuItem1_Click(object sender, EventArgs e)
            {
                Form1 f1 = new Form1();
                f1.ShowDialog();           
            }
    Friday, July 10, 2009 11:41 AM

Answers

  • Hi Tejaswini,

    > when user will again say Login..it shows the second instance of WorkArea opened..

    Do you mean you'd like to create a new instance of the WorkArea form and close the previous instance of the WorkArea form after the user relogins successfully? If so, I suggest that you set the Login form as the main form. The following is a complete sample. It requires that you create a Winform application project and add two forms named loginForm and workArea in the project. Add two Buttons on the loginForm named btnOK and btnCancel. Add a Button on the workArea named btnLogout.

    static class Program
    {
        [STAThread]
        static void Main()
        {
           Application.EnableVisualStyles();
           Application.SetCompatibleTextRenderingDefault(false)
           Application.Run(new loginForm());
         }
    }

    public partial class loginForm:Form
    {
        public loginForm()
        {
           InitializeComponent();
        }
        workArea = null;
        private void btnOK_Click(object sender, EventArgs e)
        {
            //authenticate the user here
            // ....
            // if succeed
            if(wa != null)
            {
                wa.Close();
             }
            wa =  new workArea(this);
            wa.Show();
            this.Hide();
            // if fail
            // MessageBox.Show("Invalid user. Please input user name and password again");
         }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
         }
    }

    public class workArea:Form
    {
        loginForm lf = null;
        public workArea(loginForm logform)
        {
            InitializeComponent();
            lf = logform;
         }
        private void btnLogout_Click(object sender, EventArgs e)
        {
            this.Enabled = false;
            lf.Show();
         }
    }

    Hope this helps.
    If you have any question, please feel free to let me know.

    Sincerely,
    Linda Liu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. end us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
    • Marked as answer by Linda Liu Thursday, July 16, 2009 8:41 AM
    Wednesday, July 15, 2009 8:32 AM

All replies

  • What's the point of not closing the work area form?  Creating a new instance of it is just a lot easier.

    Hans Passant.
    Friday, July 10, 2009 1:04 PM
    Moderator
  • You need to declare your instance of Form2 as a class level variable in Form1, and then check for null in your button1_Click handler.  Also you can handle the close event of Form2 and cancel the close and instead show your login form.  That way you'll only have 1 instance of each form.  The way you have it now is that Form1 create Form2, and then Form 2 creates a new Form1 which will then create a new Form2 etc


    -------------Form 1-------------

    private Form2 _f;

    private void button1_Click(object sender, EventArgs e)
    {
        if(_f==null)
        {
    _f = new Form2();
            _f.FormClosing += new FormClosingEventHander(Form2_FormClosing);
        }
        _f.Show();
        this.Hide();
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
    e.Cancel = true;
    this.ShowDialog();

    }

    ---------------- Form 2 --------------
    private void logoutToolStripMenuItem1_Click(object sender, EventArgs e)
    {
    this.Close();
    }



    If this answers your question, please mark the question as answered.
    Friday, July 10, 2009 3:59 PM

  • If you want to use the same instance of form2 again, what is the need of creating a new instance of form2??

    you can use form2 without creating any instance of it.


    Form1


     private void button1_Click(object sender, EventArgs e)
      {
      Form2.Show();
      this.Hide();
      }


    Vicky
    Monday, July 13, 2009 4:25 AM
  • Hi Tejaswini,

    > when user will again say Login..it shows the second instance of WorkArea opened..

    Do you mean you'd like to create a new instance of the WorkArea form and close the previous instance of the WorkArea form after the user relogins successfully? If so, I suggest that you set the Login form as the main form. The following is a complete sample. It requires that you create a Winform application project and add two forms named loginForm and workArea in the project. Add two Buttons on the loginForm named btnOK and btnCancel. Add a Button on the workArea named btnLogout.

    static class Program
    {
        [STAThread]
        static void Main()
        {
           Application.EnableVisualStyles();
           Application.SetCompatibleTextRenderingDefault(false)
           Application.Run(new loginForm());
         }
    }

    public partial class loginForm:Form
    {
        public loginForm()
        {
           InitializeComponent();
        }
        workArea = null;
        private void btnOK_Click(object sender, EventArgs e)
        {
            //authenticate the user here
            // ....
            // if succeed
            if(wa != null)
            {
                wa.Close();
             }
            wa =  new workArea(this);
            wa.Show();
            this.Hide();
            // if fail
            // MessageBox.Show("Invalid user. Please input user name and password again");
         }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
         }
    }

    public class workArea:Form
    {
        loginForm lf = null;
        public workArea(loginForm logform)
        {
            InitializeComponent();
            lf = logform;
         }
        private void btnLogout_Click(object sender, EventArgs e)
        {
            this.Enabled = false;
            lf.Show();
         }
    }

    Hope this helps.
    If you have any question, please feel free to let me know.

    Sincerely,
    Linda Liu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. end us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
    • Marked as answer by Linda Liu Thursday, July 16, 2009 8:41 AM
    Wednesday, July 15, 2009 8:32 AM
  • Thanks :) It's a perfect solution.
    Tuesday, August 11, 2009 11:11 AM
  • Hi Linda,
    I am an novice programmer and I do not understand that why you use the partial classes for the login form.

    As I know that Partial Classes are used to split the a class definition across multiple files. Please tell me if I am wrong.

    Thanks in advance.
    Tuesday, August 11, 2009 2:39 PM
  • breakthroughneeded, you should create a new thread when the question has already been answered.

    That is what partial classes are for, and when you create a new Form in Visual Studio it automatically adds "partial". You'll notice this code:

    public loginForm()
        {
           InitializeComponent();
        }

    InitializeComponent() is in the other file, loginForm.Designer.cs. The code is automatically generated by the designer and defines what you see in the design window. If you expand your form in the visual studio solution explorer you can see the Designer.cs file, but don't manually edit it unless you know what you're doing.
    Tuesday, August 11, 2009 3:00 PM