Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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
C#
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

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


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
Posted
Updated 5-Jun-13 21:03pm
v2

 
Share this answer
 
Comments
Ehab Nada 7-Jun-13 13:46pm    
OriginalGriff,
I read all your article/tips in the link you supplied and I’m working now on rearranging my program accordingly, I think it is going to take me sometime before I get back to you with the good news :-), meanwhile I wanted to thanks you as I have learned from your posts, it added to me valuable information.
Thanks.
Ehab
OriginalGriff 7-Jun-13 14:04pm    
You're welcome!
(That's what we write them for :laugh: )
Ehab Nada 21-Jun-13 13:43pm    
OriginalGriff,
Better late than never!! Thanks, I’m up to the next challenge in my program, actually it’s a bit complicated (at least for me :-D)I have to play with videos and access data base, but anyway I think I’m up to it
Thanks again for your help
Ehab
OriginalGriff 21-Jun-13 14:03pm    
No problem - you'll get there!
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
 
Share this answer
 
Comments
Ehab Nada 21-Jun-13 13:46pm    
Thanks Sergey for your time, your input did helped me adding more information to my experience
Ehab
Sergey Alexandrovich Kryukov 21-Jun-13 16:06pm    
You are welcome. Will you accept this answer formally then (green button)?
In all cases, your follow-up questions will be welcome.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900