0
votes

I am trying to specify the parent MDI form when showing a form in c#

All the examples suggest just using

FormVariable.Parent = this;

this works ok assuming you want the form to be opened from the parent window all the time.

I want to be able to open a form and set the Parent form to my MDI Parent form by specifying the name.

in VB.net I have used

Me.MdiParent = TheNameOfMyParentForm

When I try anything similar in c#

this.MdiParent = CruxMDI();

I get

'Crux.CruxMDI' is a 'type' but is used like a 'variable'

1
Is CruxMDI a function ? If not remove the ().Magnus
What happens when you use this.MdiParent = TheNameOfMyParentForm;?John Saunders
No CruxMDI is the parent form. I miss typed in the post.. there should have been no () So I have tried this.MdiParent = CruxMDI; and I get the error 'Crux.CruxMDI' is a 'type' but is used like a 'variable'Danny Seager
You definitely need to unlearn vb.net practices, you can't substitute a type name where an object name is required in C#. If this code is inside an MDI client form then this.Parent will work. You can use Application.OpenForms[0] if you are desperate.Hans Passant
I'm not desperate... I want the job to be done right. My parent form is called CruxMDI I always want the child forms to be a child of this parent. Can anyone tell me how to get this done and hopefully the most preferred way. (i.e. is it in the method I use to open the form or is it in the forms Load event)Danny Seager

1 Answers

1
votes

Form.MdiParent has to reference a concrete instance. So, maybe it would be a good idea to implement a Singleton pattern (you probably don't want to allow multiple parent windows either way, do you?) in your Parent container, so that you can reference it from wherever you need to. Then you'll just type:

this.MdiParent = CruxMDI.Instance;

If you want to add such behavior automatically and it needs to happen in many Forms in your application, you may consider an option when you'd create a custom base class inheriting from Form. That way you specify this once and then you just need to be sure to inherit your new Forms from this baseclass instead of a default Form.

Either way, you need to have some kind of mechanism to reference the instance of your MDI container.