简体   繁体   English

当窗口处于最大化状态时,用户控件不调整大小 - Windows Forms C#

[英]User Control Not Resizing When The Window Is In Maximized State - Windows Forms C#

Lately I am working on this personal app, it's form is without border so the Exit Button, Minimize Button and Maximize button, as well as the option to resize and move the form have been custom added.最近我正在开发这个个人应用程序,它的表单没有边框,因此自定义添加了退出按钮、最小化按钮和最大化按钮,以及调整大小和移动表单的选项。 The app contains several User Controls which perform different functions such as Login, a Control Panel for the app and so on.该应用程序包含多个用户控件,它们执行不同的功能,例如登录、应用程序的控制面板等。 The issue here is that the User Controls, which are placed within a panel ( Anchored properly, of course) in the main form are not resizing properly when the window is in a Maximized state.这里的问题是当窗口处于最大化状态时,放置在主窗体中的面板(当然正确锚定)中的用户控件没有正确调整大小。

To detail... the app starts at this size and while it's manually resized in minimized state the controls have no issue inheriting it's parent's (the panel) size, like this and this also works if the window is maximized while the control is visible , like this however if the window is already in a maximized state and I call the control with the designated button the control does not resize, it stays to it's minimum dimensions, like so .详细说明...应用程序以此大小开始,虽然它在最小化状态下手动调整大小,但控件继承它的父级(面板)大小没有问题,就像这样,如果在控件可见时最大化窗口,这也有效,像这样但是如果窗口已经处于最大化状态并且我用指定的按钮调用控件,控件不会调整大小,它会保持它的最小尺寸,就像这样

At first I thought it may have something to do with the code that resizes the form so I removed everything and made the app with the default windows border and controls, basically set the border Property from none to sizable but that did nothing.起初我以为它可能有一些做所以我删除一切,用默认的窗口边框和控件所做的应用程序,调整大小的形式,基本上是从没有border属性设置为相当大的但没有采取任何的代码。 Also I have tried accessing the User Control's parent (the panel) using this.Parent and then setting the width and height of the control with Width = this.Parent.Width and Height = this.Parent.Height but the parent returns null for some reason which I am yet to understand.此外,我尝试使用this.Parent访问用户控件的父级(面板),然后使用Width = this.Parent.WidthHeight = this.Parent.Height设置控件的宽度和高度,但父级为某些返回null我还不明白的原因。 Now, worth mentioning that those user controls are dynamically added (ie Login loginForm = new Login(); ) to the panel every time the button is clicked and then Disposed once the control is left.现在,值得一提的是,每次单击按钮时,这些用户控件都会动态添加(即Login loginForm = new Login(); )到面板,然后在离开控件后Disposed

I looked all over Google for this but found nothing related and at this point I am out of options.我在谷歌上到处找这个,但没有发现任何相关的东西,在这一点上我别无选择。 I really want the app to be resizable and must resize properly so if anyone has any solutions I am open to anything.我真的希望应用程序可以调整大小,并且必须正确调整大小,因此如果有人有任何解决方案,我愿意接受任何解决方案。

Thanks anticipated.谢谢期待。

Meanwhile, for Winforms, I may have an answer.. the key is to use the Resize event of your user control(s), see below.同时,对于 Winforms,我可能有一个答案.. 关键是使用用户控件的 Resize 事件,见下文。 I tested this in Core 3.1 and Net 4.7, you can use an default designer.cs unit with an empty form.我在 Core 3.1 和 Net 4.7 中对此进行了测试,您可以使用带有空表单的默认设计器.cs 单元。 It has no issues with resize from maximized state.从最大化状态调整大小没有问题。

My user control for the center app is named CenteredPanel and derived from Panel.我对中心应用程序的用户控件名为 CenteredPanel 并派生自 Panel。 Make sure you derive your user controls from Panel and call base() on the constructor, else the docking will not work properly.确保从 Panel 派生用户控件并在构造函数上调用 base() ,否则对接将无法正常工作。

Also, your question about parenting: it was not needed to use Parent below, but if you really need it, make sure you assign it !另外,您关于育儿的问题:下面不需要使用 Parent,但如果您真的需要它,请确保您分配它!

   public partial class Form1 : Form
    {
        private Panel panel1, panel2, panel3, panel4;
        private CenteredPanel panelApp;

        public Form1()
        {
            InitializeComponent();
            this.panel1 = new System.Windows.Forms.Panel()
              { Parent = this, Dock = DockStyle.Fill };
            this.panel2 = new System.Windows.Forms.Panel()
              { Parent = panel1, Dock = DockStyle.Left, Width = 120, BackColor = Color.FromArgb(60, 60, 60) };
            this.panel3 = new System.Windows.Forms.Panel()
              { Parent = panel1, Dock = DockStyle.Fill };
            this.panel4 = new System.Windows.Forms.Panel()
              { Parent = panel3, Dock = DockStyle.Top, Height = 60, BackColor = Color.FromArgb(60, 60, 60) };
            this.panelApp = new WindowsFormsAppCore.CenteredPanel()
              { Parent = panel3, Dock = DockStyle.Fill, BackColor = Color.FromArgb(90, 90, 90) };
            this.panel1.Controls.Add(this.panel3);
            this.panel1.Controls.Add(this.panel2);
            this.panel3.Controls.Add(this.panelApp);
            this.panel3.Controls.Add(this.panel4);
            this.Controls.Add(this.panel1);
        }
    }

    public class CenteredPanel : Panel
    {
        Label label1, label2;
        TextBox textbox1;

        public CenteredPanel() : base()
        {
            this.Resize += new System.EventHandler(this.Resizer);
            this.label1 = new System.Windows.Forms.Label()
              { Parent = this, AutoSize = true, Name = "label1", ForeColor =Color.White, Text = "Connectare Administrator" };
            this.label2 = new System.Windows.Forms.Label()
              { Parent = this, AutoSize = true, Name = "label2", ForeColor = Color.White, Text = "Nume de utilizator" };
            this.textbox1 = new System.Windows.Forms.TextBox()
              { Parent = this, AutoSize = true, Name = "textbox1", PasswordChar = '*', Text = "****" };
            this.Controls.Add(this.label1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.textbox1);
        }

        public void Resizer(object sender, EventArgs e)
        {
            Point CenteringAnchor = new Point(Width / 2, Height / 2);
            for (int i=0; i<Controls.Count; i++)
            {
                Control c = Controls[i];
                // put your resizing rules here.. this is a very simple one
                c.Location = new Point(CenteringAnchor.X - c.Width / 2, -40 + i * ((i == 1) ? 40 : 30) + CenteringAnchor.Y - c.Height / 2);
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM