WinForm窗体间传值

简介: Form1——主窗体: namespace FirstDlg{    public partial class Form1 : Form    {        private Form2 f;        public Form1()        {            Initializ...

Form1——主窗体:

namespace FirstDlg
{
    public partial class Form1 : Form
    {
        private Form2 f;
        public Form1()
        {
            InitializeComponent();
        }
        public string TextStored
        {
            get { return tbTest.Text; }
            set { tbTest.Text = value; }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            tbTest.Text = f.TextStored;//属性传值
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            f = new Form2();
            f.Show();
        }
    }
}

Form2——对话框窗体:

namespace FirstDlg
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        public string TextStored
        {
            get { return tbTest.Text; }
            set { tbTest.Text = value; }
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (Form form in Application.OpenForms)//搜索窗体名称
            {
                if (form.Name == "Form1")
                {
                    Form1 f = (Form1)form;
                    tbTest.Text = f.TextStored;
                }
            }
        }
    }
}

目录
相关文章
|
JavaScript Swift C语言
|
机器学习/深度学习 数据可视化 程序员
WinForm控件属性大全
WinForm控件属性大全
441 0
Winform窗体间传值(委托)
最近工作室为了应付 比赛 赶了一个排课系统 我负责排课的功能 写了一个星期终于赶出来了 用到了Winform窗体间传值。
993 0
|
C#
C# 窗体间传值
Form1: 父窗体, Form2: 子窗体。 1.父窗体接收子窗体的返回值: public partial class Form1: Form { private void btnOpen_Click(object sender, EventArgs e) ...
895 0