C#编程学习39:窗体间参数传递


目录

1 父窗体向子窗体传参

1.1 构造函数法

1.2 静态变量法

1.3 子窗体定义普通变量法

2 子窗体向父窗体传参

2.1 采用ShowDialog的this参数传递

2.2 父窗体赋值方法形式

2.3 父窗体定义属性

R 参考资料


核心内容概览:

 

父窗体到子窗体:构造函数法、父窗体静态变量法、子窗体共有变量法

子窗体到父窗体:控件赋值法、父窗体定义方法法、父窗体添加属性法


1 父窗体向子窗体传参

1.1 构造函数法

功能描述:

  • 父窗体中拖拽一个文本框和一个按钮
  • 子窗体拖拽一个文本框
  • 通过点击父窗体的按钮,在子窗体的文本框内显示父窗体文本框内输入的内容

父窗体代码:

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 paraFrm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonChxun_Click(object sender, EventArgs e)
        {
            FormChild fc = new FormChild(textBoxParent.Text);
            fc.ShowDialog();
        }


    }
}

子窗体代码:

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 paraFrm
{
    public partial class FormChild : Form
    {
        public FormChild()
        {
            InitializeComponent();
        }

        public FormChild(string text)
        {
            InitializeComponent();
            textBoxChild.Text = text;
        }
    }
}

1.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 paraFrm
{
    public partial class Form1 : Form
    {
        public static string myText = string.Empty;
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonChxun_Click(object sender, EventArgs e)
        {

            FormChild fc = new FormChild();
            myText = textBoxParent.Text;
            fc.ShowDialog();
        }


    }
}

子窗体

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 paraFrm
{
    public partial class FormChild : Form
    {
        public FormChild()
        {
            InitializeComponent();
        }

        public FormChild(string text)
        {
            InitializeComponent();
            textBoxChild.Text = text;
        }

        private void FormChild_Load(object sender, EventArgs e)
        {
            textBoxChild.Text = Form1.myText;
        }
    }
}

1.3 子窗体定义普通变量法

在子窗体中定义普通public变量;父窗体显示之前,对其进行赋值

父窗体

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 paraFrm
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonChxun_Click(object sender, EventArgs e)
        {

            FormChild fc = new FormChild();
            fc.txtStr = textBoxParent.Text;
            fc.ShowDialog();
        }


    }
}

子窗体

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 paraFrm
{
    public partial class FormChild : Form
    {
        public string txtStr = string.Empty;
        public FormChild()
        {
            InitializeComponent();
        }

        public FormChild(string text)
        {
            InitializeComponent();
            textBoxChild.Text = text;
        }

        private void FormChild_Load(object sender, EventArgs e)
        {
            textBoxChild.Text = txtStr;
        }
    }
}

2 子窗体向父窗体传参

2.1 采用ShowDialog的this参数传递

指定父窗体的所有者为父窗体,在子窗体中实例化父窗体,并为控件赋值

父窗体

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 paraFrm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonChxun_Click(object sender, EventArgs e)
        {
            //将父窗体设置为子窗体的拥有者
            FormChild fc = new FormChild();
            fc.ShowDialog(this);
        }


    }
}

子窗体

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 paraFrm
{
    public partial class FormChild : Form
    {
        public string txtStr = string.Empty;
        public FormChild()
        {
            InitializeComponent();
        }

        private void buttonToParent_Click(object sender, EventArgs e)
        {
            Form1 frm = (Form1)this.Owner;
            frm.Controls["textBoxParent"].Text = textBoxChild.Text;//控件的Name
        }
    }
}

2.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 paraFrm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonChxun_Click(object sender, EventArgs e)
        {

            FormChild fc = new FormChild();
            fc.ShowDialog(this);
        }
        public void setTextValue(string str)
        {
            textBoxParent.Text = str;
        }

    }
}

子窗体

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 paraFrm
{
    public partial class FormChild : Form
    {
        public string txtStr = string.Empty;
        public FormChild()
        {
            InitializeComponent();
        }
        private void buttonToParent_Click(object sender, EventArgs e)
        {
            Form1 frm = (Form1)this.Owner;           
            frm.setTextValue(textBoxChild.Text);    //调用父窗体的方法
        }
    }
}

2.3 父窗体定义属性

父窗体

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 paraFrm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonChxun_Click(object sender, EventArgs e)
        {


            FormChild fc = new FormChild();
            fc.ShowDialog(this);
        }
        //定义属性获取控件的值
        public string setTextValue 
        { 
            get {return textBoxParent.Text; }
            set {textBoxParent.Text = value;}
        }
    }
}

子窗体

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 paraFrm
{
    public partial class FormChild : Form
    {
        public string txtStr = string.Empty;
        public FormChild()
        {
            InitializeComponent();
        }


        private void buttonToParent_Click(object sender, EventArgs e)
        {
            Form1 frm = (Form1)this.Owner;   
            frm.setTextValue = textBoxChild.Text;//使用父窗体属性赋值
        }
    }
}

R 参考资料

C#小课堂之窗体间的参数传递方法

你可能感兴趣的:(C#编程学习)