我想在不是主窗体的窗体里获取主窗体的实例,就是可以用一个主窗体的类的实例来表示主窗体,怎么做啊???
小弟不才

解决方案 »

  1.   

     public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        public string TextBox1Text
            {
                set { this.textBox1.Text = value; }
                get { return this.textBox1.Text;  }
            }        private void button1_Click(object sender, EventArgs e)
            {
                Form2 frm2 = new Form2();
                frm2.Show(this);
            }
        }    public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }        private void button2_Click(object sender, EventArgs e)
            {
                Form1 frm1 = (Form1)this.Owner;
                frm1.TextBox1Text = this.textBox2.Text;
                this.Close();
            }
        }
      

  2.   

    你可以通过子窗体的构造函数把主窗体的对象传过去,这样获取到得就不是NEW出来的对象了
      

  3.   

    4楼的可以实现,有一点看不懂
    private void button1_Click(object sender, EventArgs e) 

          Form2 frm2 = new Form2(); 
          frm2.Show(this); 

    打开第2个窗口的时候你把第一个窗口的本身传过去了,但第2个窗口的构造函数是没参,他是怎么接收到那个对象的
    新人请高手不要笑我
      

  4.   

    frm2.Show(this); 
    ------
    this为form1的指针, form2.show(this);函数把form1的指针保存在form2的owner属性中