Access

How I can access my form1 from form 2.

Because i can't do the same as form 2

e.g.

Form1.ActiveForm.Hide();

Form2 cardform = new Form2();

cardform.Show();

------------------------------------------------

Now i whant to show my form1. How I will do it




Answer this question

Access

  • TI66

    In Form1 before you call .Show() for form 2:

    frmS.Owner = this; //this could be replaced with a form instance such as myForm, this = the current object, in this case Form1

    To access the members of Form1 from within Form2

    ((Form1)this.Owner).



  • David Pendrey

    No Problem

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    namespace Pilotta
    {

    public partial class Form1 : Form

    {

    Form2 cardform = new Form2();

    AboutBox1 about = new AboutBox1();

    public Form1()

    {

    InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)

    {

    /* this is a special variable that always represents the current
    * object. In this case 'this' is Form1.
    *
    * so by saying cardForm.Owner = this;
    *
    * we are in effect saying cardForm.Owner = (this instance of)Form1
    */

    cardform.Owner = this; //we can now cardform.Owner to access this(Form1)

    cardform.Show();

    }

    private void button2_Click(object sender, EventArgs e)

    {

    about.Show();

    }

    }

    }

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    namespace Pilotta

    {

    public partial class Form2 : Form

    {

    public Form2()

    {

    InitializeComponent();

    }

    private void pictureBox27_Click(object sender, EventArgs e)

    {

    }

    private void button1_Click(object sender, EventArgs e)

    {

    //the owner os already showing, as the owner is Form1

    //so we do not need to show the owner here.

    //this.Owner.Show();

    Hide(); //we can call Close() which is the same as this.Close()

    //however we are going to call hide

    //Close() destoys the object so we will get errors

    //if we try and access it later, Hide simply Hides it.

    //note that Form2 is the Class name, not the object, for your purposes you

    //need to access the object. When the process is inside the object as it is now

    //you can you this. or simply just type your function, such as Close() which is the

    //the same as this.CLose()

    //

    //When you are accesing a form that is not access, you must use the variable you

    //assigned to point to its object. So Form2 myForm = new Form2();

    //makes myForm the object of Form2, in this case use myForm.(member) to control

    //the form.

    }

    }

    }



  • sugruea

  • PiliATL

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    namespace Pilotta

    {

    public partial class Form1 : Form

    {

    //Form1 mainform = new Form1();

    Form2 cardform = new Form2();

    AboutBox1 about = new AboutBox1();

    public Form1()

    {

    InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)

    {

    //mainform.Owner = this;

    //(form1)this.Owner.Show();

    //mainform.Hide();

    cardform.Show();

    }

    private void button2_Click(object sender, EventArgs e)

    {

    about.Show();

    }

    }

    }

  • Access