aNobody
aNobody

Reputation: 117

Setting a form as an MDI child of a main form?

I have a main form application which is an MDI container. A user can press a button to make a new form pop up (not as an MDI child), and then from this new form that popped up, I want to be able to have a button that creates a different form as an MDI child.

In the main form I have:

ResSelectForm resSelectForm = new ResSelectForm();
resSelectForm.Show();

So in the resSelect form that popped up, when the user presses an OK button, I have:

ImageForm imageForm = new ImageForm();
imageForm.MdiParent = Mainform; // doesn't work
imageForm.Show();

I get the following error:

Error CS0119 'MainForm' is a type, which is not valid in the given context

Upvotes: 0

Views: 322

Answers (2)

TheLethalCoder
TheLethalCoder

Reputation: 6744

The problem you are having is you were trying to access the Mainform type and not an instance of it. To fix the problem you would have to pass the instance to the ResSelectForm constructor like so:

ResSelectForm resSelectForm = new ResSelectForm(this);

Then in the ResSelectForm constructor do this:

private Mainform _mainform; //Variable to use throughout the class

public ResSelectForm(Mainform mainform)
{
    _mainform = mainform;
}

Lastly whenever you need to access Mainform you'd access the variable, in your case like so:

imageForm.MdiParent = _mainform;

Upvotes: 1

aNobody
aNobody

Reputation: 117

I just ended up using a dialog box for it, since it's essentially the same thing: https://www.youtube.com/watch?v=8aDsXyiBLsI

Upvotes: 0

Related Questions