码迷,mamicode.com
首页 > Windows程序 > 详细

C#跨窗体传值详解

时间:2018-01-31 14:50:50      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:项目   传值   父窗体   oid   ali   sage   send   parent   string   

     一.父窗体传值给子窗体

     创建一个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

二、子窗体传值给父窗体

 

C#跨窗体传值详解

标签:项目   传值   父窗体   oid   ali   sage   send   parent   string   

原文地址:https://www.cnblogs.com/guhun/p/8391213.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!