Sami-L
Sami-L

Reputation: 5705

Calling a public method in Windows Forms

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 ?

Upvotes: 3

Views: 6396

Answers (2)

LarsTech
LarsTech

Reputation: 81655

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.

Upvotes: 4

James
James

Reputation: 9985

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.

Upvotes: 0

Related Questions