C# 表單間傳值總結

來源:互聯網
上載者:User

C# 表單間傳值總結

1、父表單傳值給子表單

在父表單中寫:

FormChild fc = new FormChil();//建立一個子表單fc.ShowDialog(this);//以上兩句即實現了子表單和父表單的聯絡textBox2.Text = fc.Str1;//已實現聯絡,可以傳值

2、子表單傳值給父表單


有兩種方法:

(1) 在子表單中寫:

   FormParent fp = (FormParent)this.Owner;//實現聯絡     textBox1.Text = fp.Str1;//已實現聯絡,可以傳值

(2)運用子表單的建構函式將父表單的值傳入(代碼如下),可傳一兩個父表單的值,如果要傳的值多可以用ArrayList,也可直接傳表單(範例程式碼直接傳表單)

總結:抓住表單對象和要傳具體值的聯絡

代碼:

using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace TestTransValue{    public partial class Form1 : Form    {        private string form1Text;            public string Form1Text        {            get { return form1Text; }                    set { form1Text = value; }        }        public Form1()        {            InitializeComponent();            form1Text = "這是來自Form1的Text";        }        private void Form1_Load(object sender, EventArgs e)        {                    }        private void button1_Click(object sender, EventArgs e)        {            Form2 f2 = new Form2();            f2.ShowDialog(this);            textBox1.Text = f2.Form2Text;//以上三句即可實現從子表單向父表單的傳值        }                    private void button2_Click(object sender, EventArgs e)        {            Form3 f3 = new Form3(this);            f3.ShowDialog(this);            textBox2.Text = f3.Form3Text;//以上三句即可實現從子表單向父表單的傳值        }    }}


using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace TestTransValue{    public partial class Form2 : Form    {        private string form2Text = "這是來自form2的Text";        public string Form2Text        {            get { return form2Text; }            set { form2Text = value; }        }        public Form2()        {            InitializeComponent();              }        private void Form2_Load(object sender, EventArgs e)        {                    }        private void button1_Click(object sender, EventArgs e)        {            Form1 f1 = (Form1)this.Owner;            textBox1.Text = f1.Form1Text;//以上兩句即可實現子表單擷取父表單的值(需要在父表單中聲明)        }    }}
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace TestTransValue{    public partial class Form3 : Form    {        private string form3Text = "這是來自form3的Text";        Form1 f1;        public string Form3Text        {            get { return form3Text; }                    set { form3Text = value; }        }        public Form3(Form1 form1)        {            InitializeComponent();            f1 = form1;        }        private void Form3_Load(object sender, EventArgs e)        {        }        private void button1_Click(object sender, EventArgs e)        {            textBox1.Text = f1.Form1Text;        }    }}

以上就是C# 表單間傳值總結的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.