how to make a function in a Form with static Main, that I can reach from another object?

J

Jeroen Ceuppens

So, i have an object x (GetImageNode.cs), that object wants to add a panel
to my mainform (Aviewer): Aviewer.MakePanel(1);

but i always get these fault:

C:\Documents and Settings\Eindwerk\Mijn documenten\BARCO\Eindwerk\Project
Files\thuis\21februari\AviewerV1\Nodes\GetImageNode.cs(42): An object
reference is required for the nonstatic field, method, or property
'AviewerV1.Aviewer.MakePanel(int)'



MAIN FUNCTION IN the FORM Aviewer

static void Main()

{

Aviewer aview=new Aviewer();

Application.Run(aview);

}



FUNCTION (in Aviewer) I WANT TO REACH

public void MakePanel(int type) ==> i can't make it static, because then i
can't use this.Controls

{

switch(type)

{

case 1: this.Controls.Add(new PanelGetImage());break;//GetImage

default: break;

}

}
 
T

Tim Wilson

You need to pass a reference to the main Form into the object "x". You could
do it when the object is created. This example assumes that the obect is
created inside of the main Form's scope:

// From somewhere inside the main Aviewer Form scope.
MyObject x = new MyObject(this);

// Inside MyObject.
private Aviewer mainForm;
public MyObject(Aviewer mainForm)
{
this.mainForm = mainForm;
}

Then you can use the reference to the main Form from inside the MyObject
instance by using the private field:
this.mainForm.MakePanel(1);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top