Click here to Skip to main content
15,908,843 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
photo

Have "ToolStripMenu" link and i need a link from "Form2" to open "Form1".
I try same code but i recieve this error: "Form that was specified to be the MdiParent for this form is not an MdiContainer. Parameter name: value".


This is code from "ToolStripMenu" to open "Form1".
C#
form1 testform= new form1();
testform.WindowState = FormWindowState.Maximized;
testform.MdiParent = this;
testform.Show();


What I have tried:

What code do i need for the red button in the picture above?
What's wrong ?
Did i miss anything?
Posted
Updated 24-Jan-20 23:54pm
v2

You'r going about this all wrong: your code is trying to open a Form1 instance in an existing instance of Form2, and that won't work (because Form2 is a MDI Child) and isn't what you wanted anyway (because you want Form1 to be a child of the existing MDI container, not Form2).

What you actually need to do in your Form2 button handler is tell the MDI Parent that it should open a new Form1 (or repurpose the existing one, that's up to it, not the Form2 instance which should not even know that Form1 even exists!)

That sound complex, but it really isn't - it uses the same event mechanism that all controls and forms do: Transferring information between two forms, Part 2: Child to Parent[^] shows you how.

Though to be honest, MDI apps are few and far between these days, they were deprecated for new applications long, long ago back in the days of Win95 and Win 98 in favour of TDI (Tabbed Document Interface) which is generally considered to be much easier for users to understand and use. You probably shouldn't be constructing new ones unless you have very, very good reasons for doing so! (And even then, a "flexible panelised" app live Visual Studio is likely to be a better solution than MDI).
 
Share this answer
 
Comments
Iulian Mihu 25-Jan-20 6:23am    
Form1 exist, Form2 exist and MainForm (with ToolStripMenu) ... what you want me is: into Form2, 1 button to open Form1.
OriginalGriff 25-Jan-20 6:46am    
That seems to be what you are asking for, yes.

The "Parent" always acts as a "go-between" between Child forms (which should not know that each other exist at all) - so you press the Form2 button, it fires an event which the Parent handles, it decides what to do with it and either opens a new instance, or activates an existing Form1.
Iulian Mihu 25-Jan-20 6:57am    
Any chance to do this with a void ? Now i try to call a void from MainForm to open Form1 button .... no success for now.
OriginalGriff 25-Jan-20 7:03am    
Follow the link I gave you, and see how that's working. It's exactly the same process, and you aren't making a lot of sense in your replies - remember we can't see your screen so we only get exactly what you type to work with.
Make sure that the form where you have the menu is an MDI container. See How to: Create MDI Parent Forms - Windows Forms | Microsoft Docs[^]
 
Share this answer
 
Comments
Iulian Mihu 25-Jan-20 5:50am    
is True.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900