C#跨窗体传值详解

一.父窗体传值给子窗体

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

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)
            {
            }
        }
    }
}

(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)
            {
            }
        }
    }
}

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;
        }
    }
}

(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

二、子窗体传值给父窗体

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

时间: 2024-08-29 23:48:40

C#跨窗体传值详解的相关文章

跨域请求详解

同源策略 Ajax的一个限制是同源策略(same origin policy),它要求所有请求必须来自同一个域名.子域名,并且地址的端口也应当一致.主要原因是处于安全考虑:因为当一个ajax请示被发送,所有的请求都会附带主域的cookie信息一起发送.也就是说,对于远程服务来讲,请求如果是来自于登陆后的用户,若没有同源策略的限制,攻击者就有可能获取你的Gmail里的邮件.得到你的 Fackbook 状态或者你 Twitter 中的好友,这是一个非常严重的安全性问题. 但是,尽管出于安全问题的考虑

C#跨窗体传值的几种方法分析第三版

窗体传值是在学习窗体应用程序时碰到的一类比较常见的问题,现将窗体传值方法做了一点总结,方法如下: <1>声明全局变量传值: 在Form1中声明全局变量,如下所示: 1 public static string str = "窗体1的值"; 通过Form1的button1_Click事件即可将此全局变量传递给Form2,Form1的全局变量str在Form2中可以直接访问,代码如下所示: 1 private void button1_Click(object sender,

AJAX请求和跨域请求详解(原生JS、Jquery)

一.概述 AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. AJAX = 异步 JavaScript 和 XML,是一种用于创建快速动态网页的技术.通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新.传统的网页(不使用 AJAX)如果需要更新内容,必需重载整个网页面. 本博客实验环境: python:2.7.11 web框架:tonado jquery:2.1.1 二.“伪”AJAX 由于

C#跨窗体传值

第一种方法: 创建一个类,里面声明用于存储接收的字段.传的时候存储于字段中,要用的时候,直接类名.字段名 进行调用.(这种方法传递是双向的) 第二种方法: 1.在Form1里定义 public string Name = "*****" 2. 在Form2里创建Form1对象, Form1 f = new Form1(); 然后就可以通过f.Name取值了 第三种方法:用构造函数 在窗体Form2中 int value1; string value2; public Form2 ( i

jsonp 跨域原理详解

JavaScript是一种在Web开发中经常使用的前端动态脚本技术.在JavaScript中,有一个很重要的安全性限制,被称为“Same-Origin Policy”(同源策略).这一策略对于JavaScript代码能够访问的页面内容做了很重要的限制,即JavaScript只能访问与包含它的文档在同一域下的内容. JavaScript这个安全策略在进行多iframe或多窗口编程.以及Ajax编程时显得尤为重要.根据这个策略,在baidu.com下的页面中包含的JavaScript代码,不能访问在

Qt 无边框、透明、可移动、的个性窗体案例详解

很多朋友都问透明的效果怎么做,为什么自己做的无边框窗体不可移动,一个个回答的很累,干脆写出来分享下好了. int main(int argc, char *argv[]){ QApplication::setStyle("cleanlooks"); QApplication a(argc, argv); login w; w.setWindowTitle("ClientLogin"); w.setWindowOpacity(1); w.setWindowFlags(

你不知道的jQuery Item11 -- ajax jsonp跨域方法详解

文章从JSON和JSONP区别开始讲起,用实例来对比他们之间的不同之处,然后详细讲解了jQuery中的ajax jsonp的使用并给出了示例及详细参数说明. 1.JSON和JSONP JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,用于在浏览器和服务器之间交换信息. JSONP(JSON With Padding),就是打包在函数调用中的的JSON(或者包裹的JSON),你要跨域请求别人的东西,你肯定要包裹起来,不要污染了别人的东西,把Json数据包裹

ios-prepareForSegue场景切换KVC传值详解

iOS开发中,通过 storyboard 可以直接切换场景,也就是在 不同的ViewController之间跳转;在跳转的过程中会自动的调用prepareForSegue方法,我们在该方法中可以 直接给 目标场景设置要传入的值;下面来介绍一下,使用KVC和普通的属性方式来传值的小Demo. 如下图,有两个 视图控制器,A和 B , A的视图控制器上有一个按钮,拖拽按钮的事件到 B 控制器上 (使用show), A在切换的过程中需要给B一个number值,B收到该值之后显示出来; A绑定的类是,V

ajax请求总是不成功?浏览器的同源策略和跨域问题详解

场景 码农小明要做一个展示业务数据的大屏给老板看,里面包含了来自自己网站的数据和来自隔壁老王的数据.那么自己网站的数据提供了 http://xiaoming.com/whoami 这样的数据接口隔壁老王提供了 http://oldwang.com/isdad 这样的数据接口单独点开都是没有问题的.但是一使用 js 的 ajax 请求就无法收到来自 oldwang.com 的数据了.点开浏览器控制台一看,红字标出(Chrome): XMLHttpRequest cannot load http:/