欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

c#父窗体子窗体之间传值

程序员文章站 2022-06-10 20:34:01
...

c#窗体传值

1. 父窗体向子窗体传值

父窗体Form1程序

//使其成为主窗体Form1的一个属性,
        private string text;
        public string Text
        {
            get { return text; }
            set { text = value; }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            text = "rfeg";
            Form2 fmChild = new Form2();
            fmChild.ShowDialog(this);
        }

子窗体Form2程序

//Form1向Form2传值
        private void Form2_Load(object sender, EventArgs e)
        {
            Form1 fmMain;
            fmMain = (Form1)this.Owner;
            textBox1.Text = fmMain.Text;
        }

2. 子窗体向父窗体传值

父窗体Form1程序

//使其成为主窗体Form1的一个属性,
        private void button1_Click(object sender, EventArgs e)
        {
            text = "rfeg";
            Form2 fmChild = new Form2();
            fmChild.ShowDialog(this);
             if (fmChild.DialogResult == DialogResult.OK)
            {
                textBox1.Text =  fmChild.Path;

            }
            else if (fmChild.DialogResult == DialogResult.Cancel)
            {
                textBox1.Text = "form was canceled";

            }
            fmChild.Close();   
        }

子窗体Form2程序

        public string Path
        {
            get { return 浏览框.Text; }
            set { 浏览框.Text = value; }
        }
         private void button2_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                浏览框.Text = folderBrowserDialog1.SelectedPath;
            }
        }