如何让孩子形式一起沟通 [英] How to get child form communicate together

查看:68
本文介绍了如何让孩子形式一起沟通的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我现在正在编写代码几年但不是专业人士,我有不同的职位,但我喜欢创建模块和编写代码,最后一个我我正在制作一个MainForm(父母)和3个儿童形式:

VideoForm:此表格有一个WMP可播放视频

CntlForm:这个表单得到了视频控制按钮(例如播放,停止...)

DBForm:连接到数据库并包含显示视频列表的数据网格并且用户可以点击播放所选视频。

我的主要问题是让所有表格一起通信;主窗体中的代码如下所示:

Hi,
I’m writing code for a couple of years now but not as a professional, I have different job title but I love to create modules and write codes, the last one I’m working on has a MainForm (Parent) and 3 childe forms:
VideoForm: this form has a WMP to play video
CntlForm: this form got the video control buttons (e.g. play, stop…)
DBForm: connected to data base and contains a data grid that show video list and the user can click on to play the selected video.
My main problem is to get all the forms communicate together; the code in the main form goes like this:

public partial class MainForm : Form
{
    // to be able to load the forms on starting the main form
    public VideoForm VDOMDIChild = new VideoForm();      //Video Form
    public CntlForm CntlMDIChild = new CntlForm ();      //controls Form
    public DBForm DBMDIChild = new DBForm ();            //Database Form



......... ...... ..

在控件表单中


……………..
In the controls form

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        //create instance of the video form
        VideoForm VDOMDIChild = (VideoForm1)Application.OpenForms["VideoForm"];  //Video  Form
        //call the stop video
        VDOMDIChild.WMPlayer1.Ctlcontrols.stop();
    }
    catch
    {
        MessageBox.Show("botton1 click", "info", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
    }
}





最后在视频表格中





And finally in the video form

public void StopVedio()
{
    WMPlayer1.Ctlcontrols.stop();
}





这是我第一次使用MDI表格来编写程序(并且它不起作用: - ))。因此,如果有人可以指导我使用停止视频作为示例让我的表单进行通信的最佳方式,我会很感激。

谢谢。

Ehab



This is the first time for me to wrie a program using MDI forms (and it is not working:-)). So if someone can guide me though the best way of having my forms communicating together using this stop video as an example I would appreciate it.
Thanks.
Ehab

推荐答案

看看这里:在两种表格之间传递信息,第3部分:儿童到儿童[提示/技巧] [ ^ ]


首先,有表单之间没有功能性的父子关系。正式地,表单 Control ,因此它具有 Parent 属性,但尝试使一个表单成为任何东西的子句将引发异常。可以通过为属性 TopLevel 分配false来更改它,但我建议永远不要这样做。另一方面,如果你有多个非模态形式(不是最好的UI风格),使用属性所有者和表单所有权关系是非常重要的。但是,我宁愿建议只有一个)。请参阅:

http:// msdn .microsoft.com / zh-cn / library / system.windows.forms.form.owner.aspx [ ^ ]。



我们还没有来你的问题。这是关于表单协作的流行问题。最强大的解决方案是在表单类中实现适当的接口,并传递接口引用而不是引用Form的整个实例。有关更多详细信息,请参阅我以前的解决方案:如何以两种形式复制列表框之间的所有项目 [ ^ ]。



另请参阅此处的其他解决方案讨论。如果应用程序足够简单,解决方案就像在一个表单中声明一些 internal 属性并将对一个表单的实例的引用传递给另一个表单的实例一样简单形成。对于更复杂的项目,这种违反严格封装的样式和松散耦合可能会增加代码的意外复杂性并引发错误,因此封装良好的解决方案将是优惠。



另请参阅:

http://en.wikipedia.org/wiki/Accidental_complexity [ ^ ],

http://en.wikipedia.org/wiki/Loose_coupling [ ^ ]。



-SA
First of all, there is no a functional parent-child relationship between forms. Formally, a Form is a Control, so it has the Parent property, but an attempt to make one form a child of anything will throw an exception. It can be changed by assigning false to the property TopLevel, but I would recommend to never do it. From the other hand, it''s very important to use the property Owner and form ownership relationship, if you have more than one non-modal form (not the best UI style though, I would rather recommend to have only one). Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.owner.aspx[^].

We did not yet came to your question. This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA


这篇关于如何让孩子形式一起沟通的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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