Click here to Skip to main content
15,074,483 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi I added a windows form inside of a small panel:
form1 f1=new form1();
f1.TopLevel=false;
panel1.Controls.Add(f1);
f1.Show();

that is OK. then I fill datagridviews and textboxes and ... in this small form, now i want maximizing this small form with a button on it, but bellow code dont worked:

this.TopLevel=true;
this.Show();

What I have tried:

form1 f1=new form1();
f1.TopLevel=false;
panel1.Controls.Add(f1);
f1.Show();

//
this.TopLevel=true;
this.Show();
Posted
Updated 22-Jun-16 1:42am
v2

As OriginalGriff explains to you here, you can remove a Panel, or UserControl, or other ContainerControl, from a Form's ControlCollection, and then add it to another Form (or UserControl's, etc.) ControlCollection.

It is not good programming practice, however, to put a Form itself inside another Form, or inside some other ContainerControl; the reasons why this is not a good idea get a bit esoteric, but I would use the analogy that you don't need a sledge-hammer to drive a nail into a wall made out of wood: a form in a form or panel is unnecessariliy "heavy."

Before you read the example shown here that moves a UserControl back and forth from MainForm to secondary Form, consider the idea that you can make a Panel, or UserControl, expand to fill the Form by setting its 'Dock Property to 'Fill, followed by using 'SendToBack() so it will cover other Controls on the Form. You can then reset the UserControl's 'Size and 'Position by setting the 'Dock Property back to 'None.

To use another Form shown 'Maximized to display a UserControl:

0. create the UserControl with the Controls you need, setting 'Anchor and 'Dock positions to create the UI you want. call it 'UserInterface1

0.a. do the right thing to expose the Controls on the UserControl, or to provide public Event Handlers that our Main Form can subscribe to when the user interacts with the Controls.

1. create the Form to hold the UserControl and show it occupying the full Form client area. call it 'FullScreenViewForm

1.a. set the form's 'WindowState property to 'Maximized.

1.b add this code to the full size view form:
C#
using System.Windows.Forms;

namespace YourNameSpace
{
    public partial class FullScreenViewForm : Form
    {
        public FullScreenViewForm()
        {
            InitializeComponent();
        }

        public UserInterface1 FullScreenViewUserControl { set; get; }

        public void SetUserInterface(UserInterface1 usercontrol)
        {
            this.FullScreenViewUserControl = usercontrol;
            this.Controls.Add(FullScreenViewUserControl);

            FullScreenViewUserControl.Dock = DockStyle.Fill;
        }
    }
}
2. place an instance of the 'UserInterface1 Usercontrol on your Main Form, and place it where you want it: 'userInterface11

2.a. put a Button on the Main Form named: 'btnSwitchUserControlViewState

2.b. wire-up this Button's Click EventHandler as shown below

3. you'll control where the instance of 'UserInterface1 appears like this in the Main Form:
C#
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace YourNameSpace
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private FullScreenViewForm fullViewForm;

        // to begin with the UserControl is on the Main Form
        private bool UCViewIsLocal = true;

        private void Form1_Load(object sender, EventArgs e)
        { 
            fullViewForm = new FullScreenViewForm();
            fullViewForm.Closing += FullViewFormOnClosing;

            // see note
            this.Closing += OnClosing;
        }

        private void OnClosing(object sender, CancelEventArgs cancelEventArgs)
        {
            // see note
            if (fullViewForm != null)
            {
                fullViewForm.FormClosing -= FullViewFormOnClosing;
                fullViewForm.Close();
            }
        }

        private void FullViewFormOnClosing(object sender, CancelEventArgs cancelEventArgs)
        {
            if (fullViewForm != null && fullViewForm.FullScreenViewUserControl != null)
            {
                this.SuspendLayout();

                fullViewForm.FullScreenViewUserControl.Dock = DockStyle.None;
                fullViewForm.Controls.Remove(fullViewForm.FullScreenViewUserControl);

                this.Controls.Add(fullViewForm.FullScreenViewUserControl);
            
                this.ResumeLayout();

                fullViewForm.Hide();

                cancelEventArgs.Cancel = true;
                
                UCViewIsLocal = true;
            }
        }

        private void btnSwitchUserControlViewState_Click(object sender, EventArgs e)
        {
            if (UCViewIsLocal)
            {
                this.SuspendLayout();

                fullViewForm.SetUserInterface(userInterface11);
                fullViewForm.Show();
                fullViewForm.BringToFront();
                
                this.ResumeLayout();

                UCViewIsLocal = false;
            }
            else
            {
                fullViewForm.Close();
            }
        }
    }
}
Notes:

1. create the "full view form" once, and then show and hide it.

2. this code relies on the "full view form" 'Closing Event to do the right thing to return the UserControl back to its site on the Main Form, but, since the code in that EventHandler always prevents the Form from closing ...

... We could rely on the fact that for a WinForms App the Main Form being closed also close all other open Forms created by the Application ...

however ... it is better programming practice to put a FormClosing EventHandler on the Main Form and to make sure we have un-plugged the EventHandler for FormClosing from the form we are using to view the UserControl expanded to full-screen size.
   
v2
Use the Dock setting:
C#
f1.TopLevel=false;
f1.Dock = DockStyle.Fill;
panel1.Controls.Add(f1);
But please, don't use a Form for this.
Instead, construct a UserControl that contains all your controls and logic and use that instead. If you also need it as an "independent" form you can create a Form object and give it just an instance of the UserControl.

Just because you can set a Form as a contained control, doesn't mean you should!
   
Comments
maysamfth 22-Jun-16 2:51am
   
so thanks but i think i dont asked my question very well!
that form showing perfect inside of panel but i want create an existing instance of form outside of panel. Suppose user enterd data but form is very small and user want see that form full screen. i want create a full screen of a form that added inside of a panel.
OriginalGriff 22-Jun-16 3:13am
   
Do you mean you want to start inside the panel, and then move it to full screen afterwards?
If not, exactly what are you trying to do? Try to describe the actual flow of events as I can't see your screen and only get what you type to work with!
maysamfth 22-Jun-16 3:20am
   
yes i want start inside the panel and then move it to full screen.
OriginalGriff 22-Jun-16 3:40am
   
Yeuch!
You can do it...but there is a good chance that things won't work properly afterwards.
To maximise it, use Panel.Controls.Remove to remove the instance from the panel, then set TopLevel to true, and WindowState to Maximized.
But...be warned. You are playing with things that really aren't designed to do this, and there is a good chance that some things will stop working, or break later.I'd strongly suggest you do this via a UserControl on the Panel, then use a separate form to display it as maximized - and copy the info from the user control to the new form user control instead of "sharing" the control between the two. It would be a lot "cleaner" and a lot less prone to errors when you modify it later.

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