C# WinForm 父窗体 子窗体 传值

本次示例效果如下:
Form1为父窗体(包含textBox1、button1)
Form2为子窗体(包含textBox2、button2)

父窗体给子窗体传值
==================
1.点击Form1的button1 打开Form2
  父窗体给子窗体传值 可以调用重载子窗体的构造函数 直接传入相关数值

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2(this.textBox1.Text);
            frm2.Show();
        }
    }

    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public Form2(string strTextBox1Text)
        {
            InitializeComponent();
            this.textBox2.Text = strTextBox1Text;
        }
    }

2.点击Form1的button1 打开Form2
  并调用子窗体Form2的公开属性或方法 将Form1的textBox1的值设置给Form2的textBox2
 
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.TextBox2Text = this.textBox1.Text;
            frm2.Show();
        }
    }

    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public string TextBox2Text
        {
            set { this.textBox2.Text = value; }
            get { return this.textBox2.Text; }
        }      
    }
 
3.点击Form1的button1 打开Form2
  在Form2_Load调用父窗体Form1的公开属性或方法 将Form1的textBox1的值设置给Form2的textBox2

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public string TextBox1Text
        {
            set { this.textBox1.Text = value; }
            get { return this.textBox1.Text;  }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.Show(this);//或 frm2.ShowDialog(this);

            或者
            //Form2 frm2 = new Form2();
            //frm2.Owner = this;
            //frm2.Show();//或 frm2.ShowDialog();
        }
    }
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            Form1 frm1 = (Form1)this.Owner;
            this.textBox2.Text = frm1.TextBox1Text;
        }
    }

子窗体给父窗体传值
==================
4.点击Form1的button1 打开Form2
  再点击Form2的button2
    在button2_Click事件中 通过this.Owner将Form2的textBox2的值设置给Form1的textBox1
    并关闭Form2

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.Show(this);//或 frm2.ShowDialog(this);

            或者
            //Form2 frm2 = new Form2();
            //frm2.Owner = this;
            //frm2.Show();//或 frm2.ShowDialog();
        }
    }

    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form1 frm1 = (Form1)this.Owner;
     //注意 如果textBox1是放在panel1中的 则先找panel1 再找textBox1
            ((TextBox)frm1.Controls["textBox1"]).Text = this.textBox2.Text;
            this.Close();
        }
    }

5.点击Form1的button1 打开Form2
  再点击Form2的button2
    在button2_Click事件中 通过this.Owner及调用父窗体Form1的公开属性或方法
                          将Form2的textBox2的值设置给Form1的textBox1
    并关闭Form2
 
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public string TextBox1Text
        {
            set { this.textBox1.Text = value; }
            get { return this.textBox1.Text;  }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.Show(this);//或 frm2.ShowDialog(this);

            或者
            //Form2 frm2 = new Form2();
            //frm2.Owner = this;
            //frm2.Show();//或 frm2.ShowDialog();
        }
    }

    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form1 frm1 = (Form1)this.Owner;
            frm1.TextBox1Text = this.textBox2.Text;
            this.Close();
        }
    }

0
0
(请您对文章做出评价)
« 上一篇: 日常生活相关网址
» 下一篇: 我们 成就了每个我的世界

<script type="text/javascript"> try { GS_googleAddAdSenseService("ca-pub-4210569241504288"); GS_googleEnableAllServices(); } catch (e) { } </script> <script type="text/javascript"> try { GA_googleAddSlot("ca-pub-4210569241504288", "cnblogs_blogpost_body"); GA_googleAddSlot("ca-pub-4210569241504288", "cnblogs_commentbox_up"); GA_googleAddSlot("ca-pub-4210569241504288", "cnblogs_blogpost_bottom"); GA_googleAddSlot("ca-pub-4210569241504288", "cnblogs_blogpost_bottom1"); } catch (e) { } </script> <script type="text/javascript"> try { GA_googleFetchAds(); } catch (e) { } </script> <script type="text/javascript"> var blog_ad_has_shown = false; </script>

posted on 2009-02-11 13:46 freeliver54 阅读(2928) 评论(9)  编辑 收藏 网摘 所属分类: WinForm 开发 , VS技術實踐

评论

#1楼 2009-02-11 21:06 no7dw[未注册用户]

一直很迷惑,看完恍然大悟~拜谢!!   回复   引用     

#2楼 2009-02-13 12:35 '[未注册用户]

uio   回复   引用     

#3楼 2009-02-16 15:02 canbingzt[未注册用户]

public partial class Form1 : Form
{
private void GetReturnValue(string text)
{
textBox1.Text=text;
}

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Form2 frm=new Form2(textBox1.Text);
frm.ReturnValue=GetReturnValue;
frm.Show();
}
}

public partial class Form2 : Form
{
public delegate void returnValue(string text);

public returnValue ReturnValue;

public Form2()
{
InitializeComponent();
}

public Form2(string text)
: this()
{
textBox1.Text=text;
}

private void button1_Click(object sender, EventArgs e)
{
if (ReturnValue != null)
{
ReturnValue(textBox1.Text);
}
Close();
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值