C#跨窗体传值详解

2021-02-17 02:18

阅读:694

     一.父窗体传值给子窗体

     创建一个Winform窗体应用程序项目,然后添加两个窗体frmChildWindow、frmParentWindow

技术分享图片

技术分享图片

技术分享图片

(1)通过Form类构造方法的重载传参

技术分享图片技术分享图片
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 DeliverValue
{
    public partial class frmParentWindow : Form
    {      
        public frmParentWindow()
        {
            InitializeComponent();
        }

        private void btnOpenChild_Click(object sender, EventArgs e)
        {
            try
            {
                string pTocStr = txtpToc.Text.Trim();
                if (string.IsNullOrEmpty(pTocStr))
                {
                    MessageBox.Show("父窗口传值子窗口值不能为空", "提示...", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                frmChildWindow _ChildWindow = new frmChildWindow(pTocStr);
                _ChildWindow.ShowDialog();
               
            }
            catch (Exception ex)
            {
            }
        }
    }
}
View Code
技术分享图片技术分享图片
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 DeliverValue
{
    public partial class frmChildWindow : Form
    {       

        public frmChildWindow()
        {
            InitializeComponent();
        }

        public frmChildWindow(string txtDeliverStr)
        {
            InitializeComponent();
            txtReceive.Text = txtDeliverStr;
        }
       
        private void btnCloseChild_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
            }
        }
    }
}
View Code

(2)通过窗体属性传参 

技术分享图片技术分享图片
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 DeliverValue
{
    public partial class frmParentWindow : Form
    {      
        public frmParentWindow()
        {
            InitializeComponent();
        }

        private void btnOpenChild_Click(object sender, EventArgs e)
        {
            try
            {
                string pTocStr = txtpToc.Text.Trim();
                if (string.IsNullOrEmpty(pTocStr))
                {
                    MessageBox.Show("父窗口传值子窗口值不能为空", "提示...", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                frmChildWindow _ChildWindow = new frmChildWindow();
                _ChildWindow.txtStr = pTocStr;
                _ChildWindow.ShowDialog();
               
            }
            catch (Exception ex)
            {
            }
        }
    }
}
View Code 
技术分享图片技术分享图片
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 DeliverValue
{
    public partial class frmChildWindow : Form
    {
        private string _txtStr;
        public string txtStr
        {
            get { return _txtStr; }
            set { _txtStr = value; }
        }

        public frmChildWindow()
        {
            InitializeComponent();
        }       
       
        private void btnCloseChild_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
            }
        }

        private void frmChildWindow_Load(object sender, EventArgs e)
        {
            txtReceive.Text = txtStr;
        }
    }
}
View Code

(3)通过Owner将父窗体属性值传参

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 DeliverValue
{
    public partial class frmParentWindow : Form
    {
        private string _parentStr;
        public string parentStr
        {
            get { return _parentStr; }
            set { _parentStr = value; }
        }
        public frmParentWindow()
        {
            InitializeComponent();
        }

        private void btnOpenChild_Click(object sender, EventArgs e)
        {
            try
            {
                parentStr = txtpToc.Text.Trim();
                if (string.IsNullOrEmpty(parentStr))
                {
                    MessageBox.Show("父窗口传值子窗口值不能为空", "提示...", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                frmChildWindow _ChildWindow = new frmChildWindow();
                _ChildWindow.ShowDialog(this);
               
            }
            catch (Exception ex)
            {
            }
        }
    }
}
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 DeliverValue
{
    public partial class frmChildWindow : Form
    {     
        public frmChildWindow()
        {
            InitializeComponent();
        }       
       
        private void btnCloseChild_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
            }
        }

        private void frmChildWindow_Load(object sender, EventArgs e)
        {
            frmParentWindow _ParentWindow = (frmParentWindow)this.Owner;
            txtReceive.Text = _ParentWindow.parentStr;
        }
    }
}

注意:_ChildWindow.ShowDialog(this);句中必须加入this

二、子窗体传值给父窗体

 


评论


亲,登录后才可以留言!