none
Help how to pass a statement from MDI Child Forms to Parent Forms. RRS feed

  • Question

  • I am using c# 2008 express edition.
    I have an MDI Child Forms trying to revive the menustripe of the MDI Parent Forms which previuosly was hiden:
    Here is the a part of the script-
           private void PsAdmin_continueBtn_Click(object sender, EventArgs e)
            {
                FrmContainer fm = new FrmContainer();
                Starter Starter_child = new Starter(fm);
                ...........
                ...........
                fm.menuStrip1.Show();//menu stripe was previously Hidden in the MDI oarent Forms
                this.Close();//the MDI child Forms  closing itself
            }
    I appreciate if there is anybody with suggestion ...
    Tuesday, October 27, 2009 4:41 PM

All replies

  • you can try,

    ((MenuStrip)this.MdiParent.Controls.Find("menuStrip1", true)[0]).Dowhateveryouwant;
    • Edited by Tamer Oz Tuesday, October 27, 2009 5:01 PM Code Corrected
    Tuesday, October 27, 2009 4:59 PM
  • Hi Tamer,
    ((MenuStrip)this.MdiParent.Controls.Find("menuStrip1", true)[0]).Show();

     is firing back an   unhandled exception.


    Tuesday, October 27, 2009 5:15 PM
  • What's the exception detail?
    Tuesday, October 27, 2009 5:43 PM
  • Hi Tamer

    here is the exception details is:
    ************* Exception Text **************
    System.NullReferenceException: Object reference not set to an instance of an object
       at Editor.Starter.PsAdmin_continueBtn_Click(Object sender, EventArgs e)

       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

    Tuesday, October 27, 2009 6:29 PM
  • Hi,

    Be sure that your MenuStrip's name is menuStrip1. And your forms MdiParent is not null.

    ((MenuStrip)this.MdiParent.Controls.Find("menuStrip1", true)[0]).Show();//here menuStrip1 is your controls name. Also check MdiParent is not null by setting a breakpoint could you canfind what expressin is resulting null.
    Tuesday, October 27, 2009 6:32 PM
  • Hi Tamer,
    MdiParent ,exists and is open and visible, if that is what you mean.
    In addition the menustripe assocated with it is correct,menuStrip1.


    Tuesday, October 27, 2009 6:48 PM
  • If you execute this code could you tell me what are the results shown in messagebox, and if gives error could you tell me which line throws exception.
    Tuesday, October 27, 2009 6:52 PM
  • here is the exception details is:

    ************* Exception Text **************
    System.NullReferenceException: Object reference not set to an instance of an object
       at Editor.Starter.PsAdmin_continueBtn_Click(Object sender, EventArgs e)

       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

    the line giving me error is: ((MenuStrip)fm.MdiParent.Controls.Find("menuStrip1", true)[0]).Show();

    i.e.
           private void PsAdmin_continueBtn_Click(object sender, EventArgs e)
            {
                FrmContainer fm = new FrmContainer();
                Starter Starter_child = new Starter(fm);
                ...........
                ...........
               ((MenuStrip)fm.MdiParent.Controls.Find("menuStrip1", true)[0]).Show();
                this.Close();//the MDI child Forms  closing itself
            }
    Tuesday, October 27, 2009 7:12 PM
  • try,

      ((MenuStrip)this.MdiParent.Controls.Find("menuStrip1", true)[0]).Show();

    If not succeeded

    execute the following and tell me results.

     

    MessageBox.Show(this.MdiParent.ToString());

     

    MessageBox.Show(this.MdiParent.Controls.Find("menuStrip1", true).Length.ToString());

     

    MessageBox.Show(((MenuStrip)this.MdiParent.Controls.Find("menuStrip1", true)[0]).ToString());

    Tuesday, October 27, 2009 7:17 PM
  • Hi Tamer,
    this is the result
           private void PsAdmin_continueBtn_Click(object sender, EventArgs e)
            {
                FrmContainer fm = new FrmContainer();
                Starter Starter_child = new Starter(fm);
                ...........
                ...........
              // ((MenuStrip)this.MdiParent.Controls.Find("menuStrip1", true)[0]).Show();                                     // (1)
                 MessageBox.Show(this.MdiParent.ToString());
                MessageBox.Show(this.MdiParent.Controls.Find("menuStrip1", true).Length.ToString());                // (2)
                MessageBox.Show(((MenuStrip)this.MdiParent.Controls.Find("menuStrip1", true)[0]).ToString());  //  (3)

                this.Close();//the MDI child Forms  closing itself
            }

    I am getting same error message on lines (1) ,(2) and (3)
    Tuesday, October 27, 2009 7:41 PM
  • I think you are passing form as Owner instead of mdiparent.

    Could you try,

    ((MenuStrip)this.Owner.Controls.Find("menuStrip1", true)[0]).Show(); 

    If it not hapens again try to set a breakpoint onclick event ans investigate where the problem is.
    Tuesday, October 27, 2009 8:05 PM
  • hi Tamer,
    I am stil experiencing same problem and the same error message. otherwise, No other problems are observed.
    Tuesday, October 27, 2009 8:41 PM
  • Hello Moksi,

    Would you please show us the code snippet about how we open the MDI child form? I suspect we did not set the MDIParent property to *this*. A code snippet to set the MDIParent might look like:
                ChildForm cf = new ChildForm();
                cf.MdiParent = this;
                cf.Show();

    Best regards,
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
    Thursday, October 29, 2009 10:04 AM
  • This is how I start my Parent Forms:
        public partial class FrmContainer : Form
        {
     
            public FrmContainer()
            {
                InitializeComponent();
            }

            private void xx()
           {
                   ...
            }
        }

    This is how I start with Child Form
    namespace Editor
    {
        public partial class logp : Form
        {
             
            public logp(FrmContainer parent)
            {
                InitializeComponent();
            }
        }
    }

    thanks
    Friday, October 30, 2009 8:46 PM
  • And now that we've gone through all of that, let's talk about doing it in a more "correct" way:

    Any time you have objects, say A and B where A knows something about B (like how to construct it, what its properties, events, methods are), we say that A is dependent on B. One of the goals of object oriented programming is to reduce dependency.

    In your case you have an MDI parent object (A) which has a dependency on some child (B). If B accesses A (by calling on a property, method, or event) you have now made B dependent on A. Since our goal is to keep dependency to a minimum, you should eliminate this dependency. One way is to use the event mechanism.

    Create an event on B that fires when needed and have A subscribe to that event and perform the operation. Object A becomes dependent on B because it has knowledge of this event, but object B needs to know nothing about A. In this case A was already dependent on B (it created it after all), so we have not added a new dependency.
    Saturday, October 31, 2009 5:56 PM
  • Please, be patient with me. It seems that Imight have not understood the interdependence between Parent and child forms.
    Here is a simple example :
    This is the Parent Form

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

    namespace test01
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    fileToolStripMenuItem .Enabled=false;
    }

    public void update()
    {
    Form1 fm = new Form1();
    child ch = new child(fm);
    ch.MdiParent = this;
    fileToolStripMenuItem.Enabled = true;
    }

    private void button1_Click(object sender, EventArgs e)
    {
    Form1 fm = new Form1();
    child ch = new child(fm);
    ch.Show();
    }
    }
    }
    Thi is MDI child forms:

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

    namespace test01
    {
    public partial class child : Form
    {
    public child(Form1 parent)
    {
    InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
    Form1 fm = new Form1();
    this.Hide();
    fm.update();

    }
    }
    }
    When Button1 is clicked, on the MDI parent ,it fires MDI child. But when Button1,on the child form, is clicked
    it fires Update() in the MDI parent. But update() is not doing what it should,that is enable the menustripe to true.
    I have problem to figureout what went wrong.

    Sunday, November 1, 2009 10:07 PM
  • Sorry this reply is so late, I just noticed your response today.

    Let me start with an analogy. You have an apple and you take a bite out of it. Now you hand the apple to me. I will receive an apple with a bite taken out of it. If I take a bite and hand it back to you, you will receive an apple with two bites missing. Following?

    Let's look at a different scenario. You have an apple and you take a bite out of it. Now you hand the apple to me, but instead of biting your apple, I get a new apple and take a bite out of that. I then return to you your original apple. How many bites are missing from your apple? Just one. The one bite you took.

    You have done scenario #2. You have created a new object and manipulated it. How are changes to this new object supposed to get reflected in the original one? They can't.
    Friday, November 13, 2009 9:52 PM