如何将一个WinForms用户控件放在三个窗体上并同时更新所有三个窗体? [英] How to Put One WinForms User Control on Three Forms and Update All Three Simultaneously?

查看:91
本文介绍了如何将一个WinForms用户控件放在三个窗体上并同时更新所有三个窗体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建用户控件



例如3个winforms和1个UserControl,



将UserControl添加到所有表单,但如果用户通过单击其中一个表单更改了UserControl内容,则所有表单都会显示相同的更改。



已编辑



我认为它现在可以正常工作。深色区域是带有某些内容的用户控件
替代文字http:// img28 .imageshack.us / img28 / 6169 / naamloostl.png



替代文字http://img36.imageshack.us/img36/7362/naamloos1bt.png

解决方案

...太长以致无法注释...如果OP请求...可能为此添加代码示例...



如果'WinForms,请在您的标签中添加一个 WinForms标签。



您可能想澄清一下:您是否在这里寻找一种可以扩展以覆盖范围的解决方案?如果您可能在UserControl上拥有许多控件(数十个?数百个?),则您需要在许多Forms上重新使用它们,所有这些都需要同步:



如果您正在(寻求扩大规模),那么我认为您需要按照Asad的建议探索 DataSource(请注意,只有某些WinForms控件(例如ComboBox等)具有一个 DataSource属性)和一个如Jake建议的 DataBinding;这两个答案都表明您需要更高级的状态同步方法。



对于小规模解决方案,您可以考虑添加设置文件,并将其用作当前值的存储库。



无论如何,您仍然需要引发一个事件……当前具有焦点的UserControl中的每一次更改……都将触发控件的更新在其他UserControl实例中。



或者,根据您的设计和要求,如果您可以接受每个UserControl都不立即更新:可以将触发更新推迟到用户切换到另一个窗体:先前处于活动状态的窗体将触发其停用事件:在该事件中,您可以检测设置是否已更改(使用UserControl中的某种脏位?),然后根据需要更新其他UserControls



另外一个好东西,恕我直言,为了得到最好的答复,在这里可能是:确切的是这些创建了多个窗体,每个窗体都包含同一UserControl的一个实例:是否有一个主窗体正在创建所有其他窗体实例:或者这是一个 S DI模型,其中每个表单都是独立创建的。



如果任何一个表单的Parent属性都设置为非null值(即,任何一个表单都有另一个Form作为Parent)?



使用公共静态类为您的问题按要求提供小规模解决方案的工作代码示例:只在这里提问,我将发布一个:它不会优雅,但可以工作:)


How to Create a UserControl which

for example 3 winforms and 1 UserControl,

adding UserControl to all forms but if user by click in one of the forms changed UserControl content, all forms display the same changes.

Edited

I think it working now. the dark area is the usercontrol with some content alt text http://img28.imageshack.us/img28/6169/naamloostl.png

alt text http://img36.imageshack.us/img36/7362/naamloos1bt.png

解决方案

... too long for a comment ... may add a code example to this if the OP requests ...

If 'WinForms, please add a "WinForms" tag to your tags.

You might want to clarify : are you looking for a solution here that will "scale up" to cover a case where you might have many controls on the UserControl (tens ? hundreds ?) you are re-using on many Forms, all of which need to be synchronized :

If you are (looking for scaling-up), then I think you will need to explore 'DataSource as Asad suggests (note that only certain WinForms controls [like ComboBox, for example] have a 'DataSource property), and 'DataBinding as Jake suggests; both those answers are pointing you to the need for a "higher-level" approach to synchronizing state.

For a "smaller scale" solution, you might consider adding a "Settings" file, and using that as a repository for current values.

In any case, you'll still need to raise an event ... with every change in the current UserControl that has focus ... to trigger updating of the controls in the other UserControl instances.

Or, depending on your design and requirements, if you can accept that each UserControl is not updated immediately : you can defer triggering update until the user switches to another Form : then the previously active Form will trigger its 'Deactivate Event : in that Event you can detect whether settings have changed (using some kind of "dirty bit" in the UserControl ?), and then update the other UserControls as needed : my guess is you do want immediate updating, however.

Another good thing, imho, to clarify in order to get the best responses here might be : exactly how are these multiple Forms, each containing an instance of the same UserControl, created : is there one "Main Form" which is creating all the other Form instances : or is this an "SDI" model where each Form is created independently. Do any of the Forms have their Parent property set to a non-null value (i.e., do any of the Forms have another Form as Parent) ?

If you want a working code example of using a public static class for a "small scale" solution to your question "as asked" : just ask here, and I'll post one : it won't be "elegant," but it will work :)

这篇关于如何将一个WinForms用户控件放在三个窗体上并同时更新所有三个窗体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆