winform程序三个窗体间同步数据(四):建立兄弟关系(不适用线程)

一 需求:

三个窗体之前同步数据:子窗体(ChildFrm)将数据同步同父窗体(ParentFrm)及兄弟窗体(ChildForm1)


 二显示效果:


三  代码

1 入口程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
       
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
           ParentFrm parentFrm = new ParentFrm();
           Application.Run(parentFrm);//启动父窗体
        }
    }
}

2 父窗体

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class ParentFrm : Form
    {
        public ParentFrm()
        {
            InitializeComponent();
        }
        public TextBox GetTbParent()
        {
            return this.TbParent;//TbParent 是私有的对象 ,所以要有公有的方法获取TbParent对象
        }
        private void button1_Click(object sender, EventArgs e)//点击事件
        {
            ChildFrm childFrm = new ChildFrm();
            ChildForm1 childForm1 = new ChildForm1();
            childFrm.parentFrm = this; //建立父子关系
            childFrm.brotherFrm = childForm1;//建立兄弟关系
            childFrm.Show();//显示子窗体
            childForm1.Show();//显示子窗体1
        }
    }
}

3 子窗体

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class ChildFrm : Form
    {
        public ParentFrm parentFrm{get;set;}
        public ChildForm1 brotherFrm { get; set; }
        public ChildFrm()
        {
            InitializeComponent();
        }

        private void TbChild_TextChanged(object sender, EventArgs e)
        {

            parentFrm.GetTbParent().Text = this.TbChild.Text;//将用户输入到子窗体TEXTBOX控件中的内容放入到父窗体的TEXTBOX控件中
            brotherFrm.getTextBox().Text = this.TbChild.Text;//将用户输入到子窗体TEXTBOX控件中的内容放入到兄弟窗体的TEXTBOX控件中
        }
    }
}

4 兄弟窗体

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    
    public partial class ChildForm1 : Form
    {
        public TextBox getTextBox()
        {
            return this.textBox1;
        }
        public ChildForm1()
        {
            InitializeComponent();
        }
    }
}
四 如何减少代码?

ParentFrm类和ChildForm1类都是继承于Form类,是否可以 将ParentFrm类的GetTbParent()函数和ChildForm1类的getTextBox()函数的函数名都统一成getTextBox(),然后利用多态减少代码?

可以。多态的实现方法有virtural关键字,和抽象类及接口  三种方式,应该采取哪种方式?

1 virtural关键字 不可以。因为我们无法修改 系统定义的类Form,在Form类里增加虚方法((即无法增加getTextBox()函数)。

2 接口的方式,可以定义一个接口,在接口里增加getTextBox()函数(请看下篇)。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值