C# .NET - Windows Forms : Hide and Show - Asked By Tejaswini Prashant J on 10-Jul-09 06:40 AM


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 inactive 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();           
        }


Windows Forms : Hide and Show - Ambrish Atlapure replied to Tejaswini Prashant J on 10-Jul-09 07:08 AM

Hi,

in Form2 add "this.close();" after f1.showDialog(), and check whether the login is valid.

re - Web Star replied to Tejaswini Prashant J on 10-Jul-09 07:17 AM

try this

Doing some winforms dev for a change - and had a need to hide a form, instead of closing it, when the user clicked the standard windows close form button (little cross [x] icon on the top right corner of the form).

The solution is to create a handler for the FormClosing event of the form, then force the form to be hidden, then, the important part, cancel the close event...

Eg:

// Use this event handler for the FormClosing event.

private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
  this.Hide();
  e.Cancel = true; // this cancels the close event.
}

Keep a reference to form1 in form2 and then call form1.Show() when form2 is closed

Make this mod to allow the owner to close the window:

private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason != CloseReason.FormOwnerClosing)
{
Hide();
e.Cancel = true; // this cancels the close event.
}
}

Then, in the owner's Form Closing, manually close the form. Application should exit then.

http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=45429
http://geekswithblogs.net/TimH/archive/2007/09/13/115323.aspx

hi - Tejaswini Prashant J replied to Ambrish Atlapure on 10-Jul-09 07:32 AM

I dnt want to close Form2, I want it should be there behind but disabled..

hey you can try the following - Ambrish Atlapure replied to Tejaswini Prashant J on 10-Jul-09 08:02 AM

Form1:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {      

        public bool blnSecondTime = false;

        public Form1()
        {           
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!blnSecondTime)
            {
                Form2 newForm2 = new Form2();
                newForm2.Show();               
            }
            this.Hide();           

        }      

    }
}


Form2:

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 f1 = new Form1();
            f1.blnSecondTime = true;
            f1.ShowDialog();           
        }
    }
}

thanks.

gr8 - Tejaswini Prashant J replied to Ambrish Atlapure on 11-Jul-09 12:48 AM
That was the simple solution...
I was trying FormClosingEventHander  and FormClosing events...
Thanks for the answer :)
Want to modify it a bit - Tejaswini Prashant J replied to Ambrish Atlapure on 14-Jul-09 08:34 AM

Hi,
As I am very new to windows forms..I am having a simple query.. plz go thru this..
My first form is Login Form which will show WorkArea Form after logged in.
Now here I have a Menu Login and Logout.
After logged in i m disabling Login Menu..and enabling Logout.
When I will click on LogOut..it should show me Login form but behind It will have WorkArea form,,as I mentioned i my previous query.
Now After Logged INagain It should have Logout enable and logout disable...
can u help me in this..
thnks in advance

JFI : Solution to the problem is here - Tejaswini Prashant J replied to Tejaswini Prashant J on 11-Aug-09 07:13 AM

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();
     }
}

How to use the same instance of a Form - Brian M replied to Tejaswini Prashant J on 21-Aug-09 01:58 AM

   The easiest way to do this is to make the Form variable (in this case "_f") a global variable. Then in the code you check to see if the variable is null or not. If it is NULL then you create the new form otherwise you simply call the Show method.

Here is an example:

        Form2 _f = null;

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

Brian

sunil replied to Tejaswini Prashant J on 02-May-11 02:35 AM

Hey Guys,
Got a better way of doing this.
using delegates you can pass values across too.


FormCollection frmcol = Application.OpenForms;

foreach (Form fo in frmcol)

{

if (fo.Name == "FormName")

{

this.Hide();

fo.Show();

}

}