C# 使用 Form Resize 调整控件大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15131779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:11:22  来源:igfitidea点击:

Resize Controls with Form Resize

c#winformsresizecontrols

提问by Kairan

I have read several stack overflow questions without finding a good working solution to my problem. How can I resize my controls whenever the form is resized? I would like them to get larger or smaller when the form becomes larger or smaller.

我已经阅读了几个堆栈溢出问题,但没有找到解决我问题的好方法。每当调整表单大小时,如何调整控件的大小?当表单变大或变小时,我希望它们变大或变小。

In visual basic this was quite easy to do with the form.Zoom property (which did't really require resizing controls of course, but solved what I needed). Unfortunately this is not available in C# winforms.

在 Visual Basic 中,使用 form.Zoom 属性很容易做到这一点(当然,它并不真正需要调整控件大小,但解决了我需要的问题)。不幸的是,这在 C# winforms 中不可用。

Here is some other things I have tried without luck:

以下是我在没有运气的情况下尝试过的其他一些事情:

private void formMain_Resize(object sender, EventArgs e)
{/*
double scale;
this.scaleWidth = (float)this.Width / (float)this.origWidth;
this.scaleHeight = (float)this.Height / (float)this.origHeight;
if (this.scaleHeight > this.scaleWidth)
{
    scale = this.scaleHeight;
}
else
{
    scale = this.scaleWidth;
}

foreach (Control control in this.Controls)
{
    control.Height = (int)(control.Height * this.scaleHeight);
    control.Width = (int)(control.Width * this.scaleWidth);
    this.Refresh();
    //  control.Font = new Font("Verdana", control.Font.SizeInPoints * heightRatio * widthRatio);
}
///////This scaling didnt work for me either
//this.Scale(new SizeF(this.scaleWidth, this.scaleHeight));
//this.Refresh();
*/
}

If I overlooked an actualy workingsample of code on another stack overflow question I would love to see it, but the ones I found were similar to those above which are not working.

如果我在另一个堆栈溢出问题上忽略了一个实际工作的代码示例,我很乐意看到它,但我发现的那些与上面那些不起作用的相似。

Perhaps I was misusing it and someone could post sample code to show for those of us who keep asking this question how to go about solving the problem.

也许我滥用了它,有人可以发布示例代码以向我们这些不断问这个问题的人展示如何解决问题。

Also, I have tried using some of the anchor/docking tools thinking they would automatically allow it but it didn't.

此外,我曾尝试使用一些锚点/停靠工具,认为它们会自动允许,但事实并非如此。

采纳答案by Kairan

I found an alternative solution that is working well for me, appreciate any negative or positive comments on the solution.

我找到了一个对我来说效果很好的替代解决方案,感谢对该解决方案的任何负面或正面评论。

Using several Split Containers and Split Containers inside of Split Containers in different regions I am able to section off the primary pieces of the layout, and within there utilizing Docking and Anchoring I am able to accomplish exactly what I wanted to do - it works beautifully.

在不同区域的 Split Containers 中使用多个 Split Containers 和 Split Containers,我能够分割布局的主要部分,在那里利用停靠和锚定我能够完成我想做的事情 - 它工作得很好。

I would point out I am aware that some folks online mention split containers use lots of resources.

我要指出的是,我知道有些人在网上提到拆分容器使用大量资源。

回答by Freddie Fabregas

Use Anchorof the control. There's an option on anchoring the top, bottom, left and right. And you're good to go.

使用控件的锚点。有一个固定顶部、底部、左侧和右侧的选项。你很高兴去。

回答by Nikola Davidovic

What you are trying to do in your code is to change the sizes of the controls which isn't so good approach. Generally, the size of the Buttons and TextBoxes shouldn't be changed when you re-size your form, but they often need to move (change location). Some controls do need to change size according to the re-sized form and but in most cases only one dimension. The central controls that are used for working area (if you are developing the tool for drawing for instance) should change sizes of both dimensions. All this you can accomplish by properly setting Dock and/or Anchor properties of the controls.

您在代码中尝试做的是更改控件的大小,这不是很好的方法。通常,当您重新调整表单大小时,不应更改按钮和文本框的大小,但它们通常需要移动(更改位置)。一些控件确实需要根据重新调整大小的表单来更改大小,但在大多数情况下只需要一维。用于工作区域的中央控件(例如,如果您正在开发绘图工具)应该更改两个维度的大小。所有这些都可以通过正确设置控件的 Dock 和/或 Anchor 属性来完成。

textBox1.Dock = DockStyle.Bottom;
textBox1.Anchor = AnchorStyles.Bottom & AnchorStyles.Left;

All these are also easily set in the Properties panel when using designer.

使用设计器时,所有这些也可以在“属性”面板中轻松设置。

But if that isn't enough for you, in rare cases, you will most definitely want to only change the location of the control:

但是,如果这对您来说还不够,在极少数情况下,您肯定只想更改控件的位置:

textBox1.Location = new Point(newX, newY);

回答by Mohammad Dehghan

The best option is to use a TableLayoutPanel. Put TableLayoutPanelon the form, set the Dockproperty to Fill, create required rows and columns and put the controls inside the cells. Of course you need to set Dock/Anchoron the controls inside the cells, so they respond to changes to the cell size. In some situations you may need to put a Panelinto a cell and drop the controls inside it, because every cell can only contain a single control. You may also need to set RowSpan/ColumnSpanon the controls.

最好的选择是使用TableLayoutPanel. 放在TableLayoutPanel表单上,将Dock属性设置为Fill,创建所需的行和列并将控件放在单元格内。当然,您需要Dock/Anchor在单元格内的控件上进行设置,以便它们响应单元格大小的更改。在某些情况下,您可能需要将 aPanel放入单元格并将控件放入其中,因为每个单元格只能包含一个控件。您可能还需要在控件上设置RowSpan/ ColumnSpan

By using a TableLayoutPanel, you have complete control over how your cotrols should be arranged. You can set absolute or percentage size for rows and columns.

通过使用TableLayoutPanel,您可以完全控制如何安排您的控制。您可以为行和列设置绝对大小或百分比大小。

回答by HillbillyBlue

If your controls are in a group box, be sure to set the group boxes properties to resize. Controls inside the box are controlled by the box. The box size (unless it is inside another box) is controlled by the form.

如果您的控件在组框中,请确保将组框属性设置为调整大小。框内的控件由框控制。框大小(除非它在另一个框内)由表单控制。