适应表单大小至 Dock 大小(或将 Dock 大小调整至表单大小)在 C# 中。

huangapple go评论70阅读模式
英文:

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

问题

I understand your request. Here's the translated content from your message:

我在C#的Windows Forms中遇到了问题,我使用了一个停靠(dock)来在顶部菜单中选择时显示多个窗体,显示的窗体与我预期的相符,但有一个小问题:窗体的最重要部分,显示保存、编辑或删除按钮的地方,即使我在显示它的函数中将其设置为填充停靠(函数名为"abrirForm"),但仍然被截断,所以我需要停靠以正确显示完整的窗体。下面是图像和相关代码。

正如您所看到的,我使用了以下图像:

这是方法,在停靠(名为"Contenedor")中显示窗体的方法,并且还更改了被按下的选项的颜色:

这是设计文件(designer.cs)中的停靠配置:

结果如下:按钮被窗口截断。

请注意,我只提供了您要求的翻译部分,没有包含其他内容。

英文:

I am having trouble with Windows Forms in C#, I used a dock in order to show several forms when they are selects in the menu on the top of the interface, and it is OK, it shows me the form I expected but there is a little detail: The most important part of the form, where are shown the buttons that will allow you save, edit or delete are cut off even when I set up the form to fill its dock in the function to show it (it is the one called abrirForm), so I need the dock showing the full form correctly. Below are the images and the relevant code.

How you can see, I am using enter image description heres

This is enter image description here

This is the method to show the form inside the dock (the one it is called "Contenedor", and also it changes the color of the option that is pressed:

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

This is the dock configuration in the designer.cs:

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

And this is the result: The buttons are cut off from the window

答案1

得分: 0

以下是翻译好的部分:

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

由于你的子控件"formulario"的大小与主窗体不同,因为菜单按钮占用了额外的空间,所以你需要找到一种自动增加添加到面板的控件的方法。幸运的是,如上所解释的,有一个名为"Autosize"的属性,当你向面板添加子控件时,永远不要将子控件的"Dock"属性设置为"Fill",因为它考虑了主控件的大小。因此,为了解决你的问题,你需要进行一些更改。

删除这行代码:

formulario.Dock = DockStyle.Fill;

为主窗体和动态添加控件的面板设置"Autoscale"属性,所以添加以下内容:

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.BackColor = Color.PaleGreen;

// 在主窗体和容器中设置"Autosize"

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

}

请注意:

如果上述解决方案不起作用,可能是因为"formulario"的代码没有提供。欢迎分享它。

英文:

In your case you can leverage the Controls Autosize property.

Since your child control formulario has not the size of the main Form because the extra space taken by the menu buttons what you need to do is look for a way to automatically increase the control added to the panel, fortunately there is a property Autosize as explained above and when you are adding a child control to a panel never set the child control dock to fill because it considers the main control size, so in order to fix your issue you need to do a few changes.

Remove this line:

> formulario.Dock = DockStyle.Fill;

Set Autoscale for the main Form and the panel where you dinamically add controls, so add this:

> this.AutoSize = true;

> Contenedor.AutoSize = true;

So your abrirForm function will look:

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

Please notice:

If above solution does not work is because the formulario code was not provided. Feel free to share it.

huangapple
  • 本文由 发表于 2023年5月14日 07:31:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245257.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定