CSharp窗体间数据传递

本文详细介绍窗体间数据传递的四种方法:使用static变量、重写构造函数、委托及Owner属性,每种方法均有具体代码示例,帮助读者理解不同场景下的适用性和优缺点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

窗体

是Form类对象,Control类的子类

实验步骤

  1. 通过父窗体打开子窗体
  2. 父窗体向子窗体传值:static变量 、重写构造函数
  3. 子窗体向父窗体传值:委托、Owner属性

static变量传值

子窗:

namespace psssv
{   
    public partial class Child : Form
    {
        public Child()
        {
            InitializeComponent();
            tChid.Text=temp.s;
        }
     public class temp
        {
            public static string s = "";
        }
}

注意temp类声明不能在Child前面,不然会报错,因为Child是Partial类。

父窗:

namespace psssv
{
    public partial class Parent : Form
    { 
        public Parent()
        {
            InitializeComponent();
        }
        private void bParent_Click(object sender, EventArgs e)
        {
            Child c = new Child();
            //static变量:
            temp.s = tParent.Text;
            c.ShowDialog();
        }
    }
}

  • 特点:双向传值,操作简单
  • 缺点:不安全。static变量在类加载时就分配内存,一般不会被销毁,除非系统内存不够,static变量才会被回收,导致静态变量访问错误

通过重写构造函数传值

子窗:

namespace psssv
{   
    public partial class Child : Form
    {
        public Child(string s)
        {
            InitializeComponent();
            tChid.Text = s;
        }
}

父窗:

namespace psssv
{
    public partial class Parent : Form
    { 
        public Parent()
        {
            InitializeComponent();
        }
        private void bParent_Click(object sender, EventArgs e)
        {
            Child c = new Child(tParent.Text);
            c.ShowDialog();
        }
    }
}

通过委托传值

委托:指向签名、返回类型相同的一组有序方法的指针
特点:适用于子窗体向父窗体传值,解耦

步骤:

  1. 声明委托
  2. 声明事件
  3. 编写事件处理方法
  4. 注册事件
  5. 触发事件

子窗:

namespace psssv
{   
    //声明委托
    public delegate void mdel(string s);
    public partial class Child : Form
    {
        //声明事件
        public event mdel m;
        public Child()
        {
            InitializeComponent();
        }
        private void bChild_Click(object sender, EventArgs e)
        {
            //触发事件
            m(tChid.Text)}   
}

父窗:

namespace psssv
{
    public partial class Parent : Form
    { 
        public Parent()
        {
            InitializeComponent();
        }
        private void bParent_Click(object sender, EventArgs e)
        {
            Child c = new Child();
            //注册事件
            c.m += ctext;
            c.ShowDialog();
        }
        private void ctext(string s)//事件处理方法
        {
            tParent.Text = s;
        }
    }
}

通过Owner属性传值

Owner:拥有此窗体的窗体

步骤:

  1. 声明Owner
  2. 获取拥有此窗体的窗体
  3. 获取包含在控件p的控件集合中的tParent属性:

子窗:

namespace psssv
{   
    public partial class Child : Form
    {
        public Child()
        {
            InitializeComponent();
        }
        private void bChild_Click(object sender, EventArgs e)
        {
            //Owner属性:
            Parent p = new Parent();
            p = (Parent) this.Owner;
            p.Controls["tParent"].Text = tChid.Text;        
        }
}

父窗:

namespace psssv
{
    public partial class Parent : Form
    { 
        public Parent()
        {
            InitializeComponent();
        }
        private void bParent_Click(object sender, EventArgs e)
        {
            Child c = new Child();
            //Owner属性:
            c.Owner = this;
            c.ShowDialog();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值