【C#】winform子窗体与父窗体传值,子窗体与子窗体传值

【1】子窗体与父窗体传值

见:http://www.cnblogs.com/xugang/archive/2010/06/29/1767465.html

【2】子窗体与子窗体传值:

前提:Form1座位父窗体,然后它new出来了两个个子窗体分别为Form2与Form3;

要求:可以及时把Form2中产生的数据,传递给Form3,更新Form3;如果Form3没有被打开,则不传递。

窗体设计:

【C#】winform子窗体与父窗体传值,子窗体与子窗体传值_第1张图片

Form1代码:

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 Test
{
    public partial class Form1 : Form
    {
        Form3 f3;
        Form2 f2 = new Form2();
        int i = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            f2.TitleChanged += f2_TitleChanged;
            f2.Show();
        }

        void f2_TitleChanged(object sender, Form2.TitleChangedEventArgs e)
        {
            if (f3 != null)
            {
                f3.setText(e.Title);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            f3 = new Form3();
            f3.Disposed += f3_Disposed;
            f3.Show();
        }

        void f3_Disposed(object sender, EventArgs e)
        {
            f3 = null;
        }
    }
}

Form2代码:

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 Test
{
    public partial class Form2 : Form
    {
        public class TitleChangedEventArgs : EventArgs // 事件参数类
        {
            private string title = "";
            public string Title
            {
                get { return title; }
                set { title = value; }
            }
        }
        // 声明委托
        public delegate void TitleChangedEventHandler(object sender, TitleChangedEventArgs e);
        // 定义事件
        public event TitleChangedEventHandler TitleChanged;
       
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            TitleChangedEventArgs e1 = new TitleChangedEventArgs();
            e1.Title = textBox1.Text;
            OnTitleChanged(e1);// 触发事件
        }
        // 触发事件的方法
        protected virtual void OnTitleChanged(TitleChangedEventArgs e)
        {
            if (TitleChanged != null)
                TitleChanged(this, e);
        }
    }
}


Form3代码:

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 Test
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {

        }
        public  void  setText(string s)
        {
            label1.Text = s;
        }
    }
}


总结:
Form2与Form3不能直接传值。 必须先将值传到Form1中,然后Form1把值传递给Form3

By:Jim


你可能感兴趣的:(C#)