3

I have 1 MDI Form which contains 1 panel control, and 1 Form with 1 button which serves to make panel in MDI not visible.

Code in MDI Form:

    public void displayInit()
    {
        panel1.Visible = false;
    }

Code in Form1:

        private void button1_Click(object sender, EventArgs e)
    {
        displayInit();
    }

The Error is: The name 'displayInit' does not exist in the current context, Any advice please ?

Sami-L
  • 5,553
  • 18
  • 59
  • 87

2 Answers2

4

Try referencing the parent (and cast it):

((MyMDIForm)this.MDIParent).displayInit();

This probably isn't the best way to do it though. Consider having the child form raise an event to the MDI parent. Separation of concerns.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • Now worked, thank you LarsTech, could you please clarify more your comment: "Consider having the child form raise an event to the MDI parent. Separation of concerns." – Sami-L Aug 15 '12 at 16:19
  • @AlphaBird Your child form shouldn't really know the details of the parent form. It will make it harder to maintain the code in the future. So instead of forcing the child form to know anything about the parent, you can have the child raise an event, something like `UpdateDisplay(...)`. When you create your child forms, you would add that event handler and when the event is called, the main form can then call `displayInit()` itself. You could change that method to private instead of public then, too. – LarsTech Aug 15 '12 at 16:27
0

The method displayinit() is an instance method, so you need an instance to call it from

MyMDIForm.displayInit();

when constructing the MDI Form keep a reference to it and use that reference when calling it's methods.

James
  • 9,774
  • 5
  • 34
  • 58
  • MyMDIForm.displayInit(); doesn't work as it is, please what do you mean by "keep a reference to your MDI Form". – Sami-L Aug 15 '12 at 16:24
  • 1
    @AlphaBird when calling a method on another form you need access to the other form, in this case you can use MDIParent as a shortcut in other cases when constructing a form you would use `MyForm FormInstance = new MyForm();` then when ever anything need to reference MyForm it would use FormInstance. Does that make sense? – James Aug 16 '12 at 08:00
  • Thank you JamesB, Yes MyForm FormInstance = new MyForm(); is useful. [Could you please tell us how to refresh FormInstance display](http://stackoverflow.com/questions/11996273/how-to-modify-a-form-and-refresh-it-from-another-form) – Sami-L Aug 16 '12 at 22:21