zoukankan      html  css  js  c++  java
  • WinForm 窗体间的传值

         父窗口:  

          private string strValueA = "";
            public string StrValueA
            {
                get
                {
                    return this.strValueA;
                }
                set
                {
                    this.strValueA = value;
                }
            }
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                this.strValueA = this.textBox1.Text;
                Form2 frmchild = new Form2();
                frmchild.Owner = this;
                frmchild.ShowDialog();
            }

            private void button2_Click(object sender, EventArgs e)
            {
                this.strValueA = this.textBox1.Text;
                Form2 frmchild = new Form2(this.textBox1.Text);
                frmchild.Owner = this;
                string returnValue = "";
                if (frmchild.ShowDialog() == DialogResult.OK)
                {
                    // 获取子窗口传回的属性值
                    returnValue = frmchild.Str;
                    this.textBox1.Text = returnValue;
                }
            }

         子窗口:

    // 设置它的变量和属性
            private string str;
            public string Str
            {
                get
                {
                    return this.str;
                }
                set
                {
                    this.str = value;
                }
            }
            // 定义一个父窗口对象
            private Form1 frmparent;

            // 默认构造函数
            public Form2()
            {
                InitializeComponent();
            }
            // 构造函数
            public Form2(string str)
            {
                this.str = str;
                InitializeComponent();
                // 赋文本框为传入的值
                this.textBox1.Text = str;
            }

            private void Form2_Load(object sender, EventArgs e)
            {
                // 将定义的父窗口对象赋值为子窗口所指向的父窗口
                frmparent = (Form1)this.Owner;
                // 同时在文本框中显示父窗口的属性值
                this.textBox1.Text = frmparent.StrValueA;
            }

            private void button1_Click(object sender, EventArgs e)
            {
                this.Str = this.textBox1.Text;
                this.DialogResult = DialogResult.OK;
                // 从子窗口中传数值给父窗口
                // this.Str = "hello";
                this.Close();
            }

  • 相关阅读:
    Leetcode python 141. 环形链表
    leetcode python 387. 字符串中的第一个唯一字符 383. 赎金信 242. 有效的字母异位词
    leetcode python 566. 重塑矩阵 118. 杨辉三角
    leetcode python 350. 两个数组的交集 121. 买卖股票的最佳时机
    小程序常见的应用场景
    小程序基础入门
    高二数学必修4
    高二数学必修3(概率)
    高中3年数学知识梳理 & 成考 专升本 高数对比;
    高一数学必修1
  • 原文地址:https://www.cnblogs.com/williamwindy/p/1379055.html
Copyright © 2011-2022 走看看