Windows Form父子两个窗体之间的传值测试

导读:本篇文章讲解 Windows Form父子两个窗体之间的传值测试,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

1:先看测试的效果图:

Windows Form父子两个窗体之间的传值测试

 

2:全部的代码

Windows Form父子两个窗体之间的传值测试

 1 using System;
 2 using System.Windows.Forms;
 3 
 4 namespace WindowsForms
 5 {
 6     public partial class ParentForm : Form
 7     {
 8         public void ParentGetvalue(string text)
 9         {
10            this.textBox1.Text = text; 
11             labelp.Text ="获取的值是:"+ text;
12         }
13         public Action<string> doinvokeP;
14         private void ParentForm_Load(object sender, EventArgs e)
15         {
16             ChildForm cf = new ChildForm(this);
17             doinvokeP += cf.ChildGetValue;  cf.Show();
18         }
19         private void btnParent_Click(object sender, EventArgs e)
20         {
21             if (doinvokeP != null)
22             {
23                 doinvokeP.Invoke(textBox1.Text);
24             }
25         }
26 
27         public ParentForm()
28         {
29             InitializeComponent();
30         }
31     }
32 }

View Code

Windows Form父子两个窗体之间的传值测试

 1 using System;
 2 using System.Windows.Forms;
 3 
 4 namespace WindowsForms
 5 {
 6     public partial class ChildForm : Form
 7     {
 8         public ParentForm cpform;
 9         public void ChildGetValue(string msg)
10         {
11             textBoxC.Text = msg;
12         }
13         public ChildForm(ParentForm cpform)
14         {
15             this.cpform = cpform;
16             InitializeComponent();
17         }
18 
19         private void btnChild_Click(object sender, EventArgs e)
20         {
21             if (cpform!=null)
22             {
23                 this.cpform.ParentGetvalue(textBoxC.Text);
24             }
25         }
26     }
27 }

View Code

 

3:总结

 由父到子窗体使用了委托,但是反过来由子到父,一样对应的逻辑,就是不行,后来调式打印,值都传递过去了,就是不行,浪费了一些时间!

     最后发现是父窗体根本就不是同一个对象的问题,看来还是要细心才行,欢迎大家有更好的建议,谢谢!

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/64017.html

(1)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!