将锚定按钮保持在自动调整大小的标签下方
Keep Anchored Buttons Below Autosized Label
我有一个带有图像的 Windows 表单、一个包含错误消息的标签和一个 "Close" 按钮:
错误消息可能很短或很长,具体取决于错误内容。当它很长时,我希望 window 增加高度以显示整个消息。使用标签的 Anchor
和 MaximumSize
属性以及表单的 AutoSize
属性,我能够让标签扩展到最大宽度,然后垂直扩展直到消息显示。将按钮的 Anchor
属性 设置为 Bottom, Right
可按预期将按钮锁定在右下角,但表单仅扩展到足以显示标签而不足以显示按钮。所以按钮显示在标签后面,看不到:
我想知道如何拉伸表单以完全包含标签文本并在底部留出空间供锚定按钮生成如下内容:
将 3 个面板添加到停靠在左侧、底部和填充的表单中。如下图所示设置属性。
然后在属性中将Label的最大宽度设置为固定值或者你可以在运行时计算:
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Me.Label1.MaximumSize = New Size(Me.panelFill.Width, 0)
End Sub
最简单的方法是使用 3 个停靠面板构建布局,如下所示(希望您可以根据需要进行调整):
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Samples
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var form = new Form { Padding = new Padding(8), AutoSizeMode = AutoSizeMode.GrowAndShrink, AutoSize = true, Font = new Font("Consolas", 9, FontStyle.Bold) };
var contentPanel = new Panel { Dock = DockStyle.Fill, Parent = form, AutoSizeMode = AutoSizeMode.GrowAndShrink, AutoSize = true };
var imagePanel = new Panel { Dock = DockStyle.Left, Parent = form };
var buttonPanel = new Panel { Dock = DockStyle.Bottom, Parent = form };
var image = new PictureBox { BackColor = Color.Red, Width = 32, Height = 32, Parent = imagePanel };
imagePanel.Width = image.Width + 8;
var button = new Button { Top = 8, AutoSize = true, BackColor = Color.Green, ForeColor = Color.White, Text = "Test", Parent = buttonPanel };
button.Left = buttonPanel.DisplayRectangle.Right - button.Width;
buttonPanel.Height = button.Height + 8;
button.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
var label = new Label { Dock = DockStyle.Fill, BackColor = Color.Blue, ForeColor = Color.White, MaximumSize = new Size(300, 0), AutoSize = true, Parent = contentPanel };
label.Text = @"The error message could be very short or very long depending on what the error is. When it's long, I want the window to grow in height to display the whole message. Using the label's Anchor and MaximumSize properties and the form's AutoSize property, I was able to get the Label to expand to the maximum width and then expand vertically until the message is displayed. Setting the button's Anchor property to Bottom, Right locks the button to the bottom right corner as intended but the form only expands far enough to display the label and not enough for the button too. So the button is displayed behind the label and can't be seen:";
Application.Run(form);
}
}
}
结果:
我有一个带有图像的 Windows 表单、一个包含错误消息的标签和一个 "Close" 按钮:
错误消息可能很短或很长,具体取决于错误内容。当它很长时,我希望 window 增加高度以显示整个消息。使用标签的 Anchor
和 MaximumSize
属性以及表单的 AutoSize
属性,我能够让标签扩展到最大宽度,然后垂直扩展直到消息显示。将按钮的 Anchor
属性 设置为 Bottom, Right
可按预期将按钮锁定在右下角,但表单仅扩展到足以显示标签而不足以显示按钮。所以按钮显示在标签后面,看不到:
我想知道如何拉伸表单以完全包含标签文本并在底部留出空间供锚定按钮生成如下内容:
将 3 个面板添加到停靠在左侧、底部和填充的表单中。如下图所示设置属性。
然后在属性中将Label的最大宽度设置为固定值或者你可以在运行时计算:
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Me.Label1.MaximumSize = New Size(Me.panelFill.Width, 0)
End Sub
最简单的方法是使用 3 个停靠面板构建布局,如下所示(希望您可以根据需要进行调整):
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Samples
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var form = new Form { Padding = new Padding(8), AutoSizeMode = AutoSizeMode.GrowAndShrink, AutoSize = true, Font = new Font("Consolas", 9, FontStyle.Bold) };
var contentPanel = new Panel { Dock = DockStyle.Fill, Parent = form, AutoSizeMode = AutoSizeMode.GrowAndShrink, AutoSize = true };
var imagePanel = new Panel { Dock = DockStyle.Left, Parent = form };
var buttonPanel = new Panel { Dock = DockStyle.Bottom, Parent = form };
var image = new PictureBox { BackColor = Color.Red, Width = 32, Height = 32, Parent = imagePanel };
imagePanel.Width = image.Width + 8;
var button = new Button { Top = 8, AutoSize = true, BackColor = Color.Green, ForeColor = Color.White, Text = "Test", Parent = buttonPanel };
button.Left = buttonPanel.DisplayRectangle.Right - button.Width;
buttonPanel.Height = button.Height + 8;
button.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
var label = new Label { Dock = DockStyle.Fill, BackColor = Color.Blue, ForeColor = Color.White, MaximumSize = new Size(300, 0), AutoSize = true, Parent = contentPanel };
label.Text = @"The error message could be very short or very long depending on what the error is. When it's long, I want the window to grow in height to display the whole message. Using the label's Anchor and MaximumSize properties and the form's AutoSize property, I was able to get the Label to expand to the maximum width and then expand vertically until the message is displayed. Setting the button's Anchor property to Bottom, Right locks the button to the bottom right corner as intended but the form only expands far enough to display the label and not enough for the button too. So the button is displayed behind the label and can't be seen:";
Application.Run(form);
}
}
}
结果: