Adapt form to Dock size (or Dock size to the size of the dock) in C#

问题描述 投票:0回答:1

我在使用 C# 中的 Windows 窗体时遇到问题,我使用了一个停靠栏以便在界面顶部的菜单中选择多个窗体时显示它们,没关系,它显示了我期望的窗体但是有一个小细节:表单最重要的部分,其中显示了允许您保存、编辑或删除的按钮,即使当我设置表单以在功能中填充其停靠栏以显示它时(它是一个叫做 abrirForm),所以我需要正确显示完整表格的停靠栏。下面是图片和相关代码。

怎么看,我在用enter image description heres

这是enter image description here

这是在 dock 中显示表单的方法(称为“Contenedor”的方法,它还会更改按下的选项的颜色:

private void abrirForm(IconMenuItem menu, Form formulario)
        {
            if(MenuActivo != null) {
                MenuActivo.BackColor = Color.White;
            }
            menu.BackColor = Color.Silver;
            MenuActivo = menu;

            if(FormularioActivo != null)
            {
                FormularioActivo.Close();
            }
            FormularioActivo = formulario;
            formulario.TopLevel = false;
            formulario.FormBorderStyle = FormBorderStyle.None;
            formulario.Dock = DockStyle.Fill;
            formulario.BackColor = Color.PaleGreen;

            Contenedor.Controls.Add(formulario);
            formulario.Show();
        }

这是designer.cs中的dock配置:

      this.Contenedor.AllowDrop = true;
      this.Contenedor.Cursor = System.Windows.Forms.Cursors.Default;
      this.Contenedor.Dock = System.Windows.Forms.DockStyle.Fill;
      this.Contenedor.Location = new System.Drawing.Point(0, 132);
      this.Contenedor.Name = "Contenedor";
      this.Contenedor.Size = new System.Drawing.Size(918, 503);
      this.Contenedor.TabIndex = 3;
      this.Contenedor.Paint += new System.Windows.Forms.PaintEventHandler(this.Contenedor_Paint);

这是结果:The buttons are cut off from the window

c# .net winforms desktop-application
1个回答
0
投票

在您的情况下,您可以利用 Controls Autosize 属性。

由于您的子控件公式没有主窗体的大小,因为菜单按钮占用的额外空间您需要做的是寻找一种方法来自动增加添加到面板的控件,幸运的是有一个属性 Autosize as上面解释过,当您将子控件添加到面板时,永远不要将子控件停靠栏设置为填充,因为它考虑了主控件的大小,因此为了解决您的问题,您需要做一些更改。

删除这一行:

formulario.Dock = DockStyle.Fill;

为主窗体和动态添加控件的面板设置自动缩放,所以添加:

this.AutoSize = true;

Contenedor.AutoSize = true;

因此您的 abrirForm 函数将如下所示:

private void abrirForm(IconMenuItem menu, Form formulario)
        {
            if(MenuActivo != null) {
                MenuActivo.BackColor = Color.White;
            }
            menu.BackColor = Color.Silver;
            MenuActivo = menu;

            if(FormularioActivo != null)
            {
                FormularioActivo.Close();
            }
            FormularioActivo = formulario;
            formulario.TopLevel = false;
            formulario.FormBorderStyle = FormBorderStyle.None;
            //formulario.Dock = DockStyle.Fill; I COMMENTED IT, YOU NEED TO REMOVE IT
            formulario.BackColor = Color.PaleGreen;

           //SET THE AUTOSIZE in Main form and Container


                this.AutoSize = true;
                Contenedor.AutoSize = true;
                Contenedor.Controls.Add(formulario);
                formulario.Show();
            }

请注意:

如果上述解决方案不起作用是因为未提供公式代码。欢迎分享。

© www.soinside.com 2019 - 2024. All rights reserved.