CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Nov 2011
    Posts
    19

    How to control visibilty of form1 from form2

    hello i'm trying to do a simple little thing, i have two forms in my application, at first form1 comes into view,then i press a button and form2 comes.all i want is that when form2 is visible form1 will be hidden. Then when i close form2, form1 will come into view.how can i accomplish this thing?

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How to control visibilty of form1 from form2

    Hide the first form on the line before you show the second form.
    Show the second form as dialog so it will block the code in the first form.
    On the next line show the first form. This line will execute when the second form has been closed.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Jan 2012
    Posts
    4

    Re: How to control visibilty of form1 from form2

    /*Consider Form1 is calling Form2. Below is the code segment you'll place in Form1 where when you click a button 'button1' an event is called 'button1_Click' and the Form1 is hidden and Form2 is displayed. I have also additionally shown how to transfer some values from one form to another*/

    private void button1_Click(object sender, EventArgs e)
    {
    parameter_being_passed=0; /*you can have any value here which you just in case need to transfer from one form to another. Here lets keep this value of data type 'int'*/


    this.Hide();
    Form2 obj = new Form2(parameter_being_passed);
    obj.Show();
    }


    /*Below will be the code-behind for Form2 for an event called when clicking button2*/

    private void button2_Click(object sender, EventArgs e)
    {
    this.Hide();
    Form1 obj = new Form1(); //doesn't matter if you use same object name 'obj'
    obj.Show();
    }


    /*Please note that as Form2 is receiving a value passed by Form1 we will need to keep the following structure for the constructor of Form2*/

    public Form2(int parameter_being_received)
    {
    InitializeComponent();
    some_datamember = parameter_being_received; // some_datamember will be of data type int
    }

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How to control visibilty of form1 from form2

    Code:
    private void button2_Click(object sender, EventArgs e)
    {
       this.Hide();
       Form1 obj = new Form1(); //doesn't matter if you use same object name 'obj'
       obj.Show();
    }
    This would create a second instance of the Form which is not what I would think the user is attempting to do.
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Jan 2012
    Posts
    4

    Re: How to control visibilty of form1 from form2

    Quote Originally Posted by DataMiser View Post

    This would create a second instance of the Form which is not what I would think the user is attempting to do.
    I suppose you are right. I have assumed the forms to be new instances at the time of creation.

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How to control visibilty of form1 from form2

    Since the OP indicated that the intent was to hide the first form when second form is loaded then make the first form visible again after the second form is closed then I would assume there will only be 1 instance of each form.

    The simple way would be to make form2 show as a dialog after hiding form1 then make form1 visible on the next line which would execute once form2 is closed.

    Optionaly a method could be created in form1 that will be called from form2
    Always use [code][/code] tags when posting code.

  7. #7

    Re: How to control visibilty of form1 from form2

    Simple example that gives you a reference to anything in Form1 from Form2:

    Code:
            // In Form1 add this where you launch Form2
            private Form2 myForm;
            myForm = new Form2();
            myForm.myCaller = this;
            myForm.Show();
    
            // In Form2 add this:
            private Form1 MyCaller;
            public Form1 myCaller
            {
                get
                {
                    return MyCaller;
                }
                set
                {
                    MyCaller = value;
                }
            }
    
            // In your button logic or someplace else in Form2 add:
            MyCaller.Hide();
    MyCaller is a reference to anything in Form1 from Form2. Before Form2 goes away, do a MyCaller.Show()

    This also allows you to share and use methods, events, classes and variables, between Form1 and Form2.

    If you had multiple Form2 needs when a Form2 is created you could assign unique Id's to each copy of Form2 by adding this:

    Code:
            // In Form2
            private int MyId = -1;
            public int myId
            {
                get
                {
                    return MyId;
                }
                set
                {
                    MyId = value;
                }
            }
    
            // In Form1 where you set myCaller
            myForm.myCaller = this;
            myForm.myId = 3;
            myForm.Name = "Form2";
    
                    // Somewhere in Form1
                    foreach (Form f in System.Windows.Forms.Application.OpenForms)
                    {
                        if (f.Name == "Form2")
                        {
                           // Your code here
                           if(f.myId == 3)
                           {
                           }
                        }
                    }
    
                   // Or even have this in Form1
                   public void Form2CopyNeedsHelp(int Form2Id)
                   {
                      // Your code here
                   }
    
                   // In Form2
                   MyCaller.Form2CopyNeedsHelp(MyId);
    Last edited by ZOverLord; January 14th, 2012 at 04:18 PM.

  8. #8
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: How to control visibilty of form1 from form2

    How about using the existing events. This code uses the formclosed event of form2. You don't need to do anything special in form2. All the code is in form1.

    Code:
        public partial class Form1 : Form
        {
            Form2 newForm2;
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                newForm2 = new Form2();
                newForm2.FormClosed += new FormClosedEventHandler(newForm2_FormClosed);
                newForm2.Show();
                this.Hide();
            }
    
            void newForm2_FormClosed(object sender, FormClosedEventArgs e)
            {
                this.Show();
            }
        }

  9. #9

    Re: How to control visibilty of form1 from form2

    Yes, I was showing methods to create a linkage from Form2 to Form1 so that things like this could be done after Form2 was started, if needed. Without restricting that use, to event monitoring, only located in Form1.

    For Example, Form2 may wish to hide/show Form1 and not close and maybe even just hide, sometime after Form2 started. Form1 could then check if there is already a running/hidden Form2, pass data to it, using a Form2 method, let it then show itself, using the new data, or start a new Form2, if needed. If Form2 is being used very often, this maybe faster than always creating a new Form2.

    This also allows the inverse of your example and allows Form2 to monitor the closing of Form1: MyCaller.FormClosed += new FormClosedEventHandler(Form1_FormClosed);

    Of course, one would want to make sure to unset this event when/if Form2 closes for any reason or exception.

    In any case, the linkage code, is a small number of lines of code and has many side benefits. Creating a full and real-time linkage between Form2 and Form1 in this example.
    Last edited by ZOverLord; January 14th, 2012 at 08:54 PM.

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