窗体间传递复杂数据
源代码下载请点这里
在设计窗体程序时往往需要相互调用的窗体间传递复杂的数据,有时候甚至需要子窗体修改父窗体的内容。前一阵在博客园中看到很多人讨论这个问题,在海天一鸥《窗体间传值和窗体间互操作》的评论中,我看到有这么几种做法:1)公开一个静态变量;2)在子窗体中创建一个公有字段;3)在父窗体中使用委托与事件;4)将子窗体作为父窗体成员。
这些办法我感觉都不是特别好,会导致父窗体与子窗体耦合过于紧密,对任何一个窗体的修改需要重新编译另外一个窗体。根据“依赖倒置”的原则,通过引 入一个结果对象,就可以避免这种紧耦合,同时也可以传递任意复杂的数据。如果需要在子窗体中改变父窗体状态,也可以在这个结果对象中定义委托与事件来达到 目的。我在这里给出我的解决方案。
首先定义一个结果对象,用来存放子窗体返回的结果。同时定义一些事件,可以让子窗体修改父窗体的状态。代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
添加一子窗体构造函数,允许接收一结果对象:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
在父窗体中创建子窗体,并订阅cResult事件:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
这样确保父窗体知道子窗体,而子窗体不知道父窗体。父窗体改变后不需要重新编译子窗体。同时两个窗体都依赖于结果对象,结果对象的稳定性也决定了父窗体与子窗体关系的稳定性。下面是程序运行结果:
![](https://images.cnblogs.com/cnblogs_com/zhenyulu/Pic24.gif)
注:提供的代码仅仅是功能演示,如果实际使用需要添加一些额外辅助代码(对象释放、取消事件订阅等)。