ShmuelCohen
ShmuelCohen

Reputation: 472

opeing form in other form , C# windows forms

im trying to make a main screen that i open all my others forms in it , and i want it will be elegant , so i did something like that

enter image description here

and the code is like that

   this.IsMdiContainer = true;
            City CityForm = new City();
            CityForm.MdiParent = this;
            CityForm.Show();
            CityForm.WindowState = FormWindowState.Maximized;

but the problem is when i open another form and another form .. all the forms stay and i think is not effective and smart

so someone have another way to do it ?

Upvotes: 1

Views: 225

Answers (5)

Haden693
Haden693

Reputation: 174

You need to 'dock' the form into the MDI Container;

City CityForm f1 = new City();
City.MdiParent = this;
City.Dock = DockStyle.Fill;
City.Show();

Then you can just close the forms when you wish.

However, if you want it done automatically then i'd look into a tabbed interface as Thorsten Dittmar has suggested.

Upvotes: 0

Alex Anderson
Alex Anderson

Reputation: 860

Try to use CityForm.ShowDialog() instead of CityForm.Show().

Upvotes: 0

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56727

Did you consider using a tabbed interface for this? Something like the editor part of Visual Studio where you have all open files (source code and forms) as single tabs? Personally I consider this far more elegant than an MDI interface.

To do this, create UserControls instead of forms for your content. When a new "View" should be opened, add a tab to the TabControl and create an instance of the respective UserControl. Add this UserControl to the tab and set the Dock property to Fill.

This is very easy and the users today are more aquainted to tabbed interfaces than to MDI interfaces.

Upvotes: 4

Leo Chapiro
Leo Chapiro

Reputation: 13979

I would work with TabControl, it's an elegant way to present several forms on the same place: http://msdn.microsoft.com/library/system.windows.forms.tabcontrol.aspx

A TabControl contains tab pages, which are represented by TabPage objects that you add through the TabPages property. The order of tab pages in this collection reflects the order the tabs appear in the control. The user can change the current TabPage by clicking one of the tabs in the control.

Upvotes: 2

Adriaan Stander
Adriaan Stander

Reputation: 166606

You will have to keep a single copy of City CityForm, and reuse that each time you wish to create a new form (so as a member variable)

Have a look at Form.IsMdiContainer Property and the use of Form mdiChildForm which should be close to how you should use City CityForm

Upvotes: 2

Related Questions