C#Winform 父窗體 子窗體 傳值

本篇博客轉載:http://www.cnblogs.com/freeliver54/archive/2009/02/11/1388173.htmlhtml

本次示例效果以下:
Form1爲父窗體(包含textBox一、button1)
Form2爲子窗體(包含textBox二、button2)函數

父窗體給子窗體傳值
==================
1.點擊Form1的button1 打開Form2
  父窗體給子窗體傳值 能夠調用重載子窗體的構造函數 直接傳入相關數值this

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }spa

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2(this.textBox1.Text);
            frm2.Show();
        }
    }orm

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

        public Form2(string strTextBox1Text)
        {
            InitializeComponent();
            this.textBox2.Text = strTextBox1Text;
        }
    }blog

2.點擊Form1的button1 打開Form2
  並調用子窗體Form2的公開屬性或方法 將Form1的textBox1的值設置給Form2的textBox2
 
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }事件

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.TextBox2Text = this.textBox1.Text;
            frm2.Show();
        }
    }get

    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }博客

        public string TextBox2Text
        {
            set { this.textBox2.Text = value; }
            get { return this.textBox2.Text; }
        }       
    }
  
3.點擊Form1的button1 打開Form2
  在Form2_Load調用父窗體Form1的公開屬性或方法 將Form1的textBox1的值設置給Form2的textBox2

    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);//或 frm2.ShowDialog(this);

            ////或者
            //Form2 frm2 = new Form2();
            //frm2.Owner = this;
            //frm2.Show();//或 frm2.ShowDialog();
        }
    }
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            Form1 frm1 = (Form1)this.Owner;
            this.textBox2.Text = frm1.TextBox1Text;
        }
    }

子窗體給父窗體傳值
==================
4.點擊Form1的button1 打開Form2
  再點擊Form2的button2 
    在button2_Click事件中 經過this.Owner將Form2的textBox2的值設置給Form1的textBox1
    並關閉Form2

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.Show(this);//或 frm2.ShowDialog(this);

            ////或者
            //Form2 frm2 = new Form2();
            //frm2.Owner = this;
            //frm2.Show();//或 frm2.ShowDialog();
        }
    }

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

        private void button2_Click(object sender, EventArgs e)
        {
            Form1 frm1 = (Form1)this.Owner;
     //注意 若是textBox1是放在panel1中的 則先找panel1 再找textBox1
            ((TextBox)frm1.Controls["textBox1"]).Text = this.textBox2.Text;
            this.Close();
        }
    }

5.點擊Form1的button1 打開Form2
  再點擊Form2的button2 
    在button2_Click事件中 經過this.Owner及調用父窗體Form1的公開屬性或方法 
                          將Form2的textBox2的值設置給Form1的textBox1
    並關閉Form2
 
    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);//或 frm2.ShowDialog(this);

            ////或者
            //Form2 frm2 = new Form2();
            //frm2.Owner = this;
            //frm2.Show();//或 frm2.ShowDialog();
        }
    }

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

相關文章
相關標籤/搜索