最大化在面板中托管的表单 [英] Maximize a form that hosted in a panel

查看:50
本文介绍了最大化在面板中托管的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在一个小面板中添加了一个窗体:

form1 f1 = new form1();

f1.TopLevel = false;

panel1.Controls.Add(f1);

f1.Show();



没关系。然后我填写datagridviews和textboxes和...这个小形式,现在我想要用它上面的按钮最大化这个小形式,但是下面的代码不起作用:



this.TopLevel = true;

this.Show();



我尝试过:



form1 f1 = new form1();

f1.TopLevel = false;

panel1.Controls.Add( f1);

f1.Show();



//

this.TopLevel = true;

this.Show();

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();

推荐答案

使用Dock设置:

Use the Dock setting:
f1.TopLevel=false;
f1.Dock = DockStyle.Fill;
panel1.Controls.Add(f1);

但请不要使用表格。

相反,构造一个包含所有控件的UserControl和逻辑,并使用它。如果你还需要它作为一个独立表单,你可以创建一个Form对象,并只给它一个UserControl的实例。



只是因为你可以设置一个表单作为一个包含的控件,并不意味着你应该!

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!


正如OriginalGriff在这里向你解释的那样,你可以从Form的ControlCollection中删除一个Panel,UserControl或其他ContainerControl,然后将它添加到另一个表单(或UserControl等)ControlCollection。



然而,将表单本身放在另一个表单或内部是不好的编程习惯一些其他的ContainerControl;为什么这不是一个好主意的原因有点深奥,但我会用你不需要用大锤将钉子钉入用木头做成的墙壁的类比:形式或面板的形式是不必要的沉重的。



在您阅读此处显示的示例之前,将UserControl从MainForm来回移动到辅助表单之前,请考虑您可以制作Panel的想法或者UserControl,通过将其Dock属性设置为Fill来扩展以填充表单,然后使用SendToBack(),以便它将覆盖表单上的其他控件。然后,您可以通过将Dock属性设置回无来重置UserControl的大小和位置。



要使用另一个显示'最大化以显示UserControl的表单:



0.使用您需要的控件创建UserControl,设置'Anchor和'Dock位置以创建您想要的UI。称之为'UserInterface1



0.a.做正确的事情来公开UserControl上的控件,或提供我们的主表单可以在用户与控件交互时订阅的公共事件处理程序。



1创建表单以保存UserControl并显示它占据完整的表单客户区域。称之为'FullScreenViewForm



1.a.将窗体的'WindowState属性设置为'最大化。



1.b将此代码添加到完整大小的视图窗体:
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:
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。在您的主窗体上放置一个'UserInterface1 Usercontrol实例,并将其放在您想要的位置:'userInterface11



2.a.在主窗体上放一个名为:'btnSwitchUserControlViewState



2.b.连接这个Button的Click EventHandler,如下所示



3.你将控制'UserInterface1实例在主窗体中出现的位置:

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:

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();
            }
        }
    }
}

注意:



1.创建完整视图表单一次,然后显示和隐藏它。



2.此代码依赖于完整视图表单'结束事件做正确的事情将UserControl返回到Main Form上的站点,但是,因为EventHandler中的代码总是阻止表单关闭...



...我们可以依赖于这样一个事实:对于WinForms应用程序,主窗体关闭也关闭应用程序创建的所有其他打开的窗体...



但是...它更好编程实践将FormClosing EventHandler放在Main Form上,并确保我们从我们用来查看扩展到全屏大小的UserControl的表单中取消插入FormClosing的EventHandler。

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.


这篇关于最大化在面板中托管的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆