【问题标题】:Keep Anchored Buttons Below Autosized Label将锚定按钮保持在自动大小的标签下方
【发布时间】:2016-01-26 00:10:08
【问题描述】:

我有一个带有图像、包含错误消息的标签和“关闭”按钮的 Windows 窗体:

错误消息可能很短或很长,具体取决于错误是什么。当它很长时,我希望窗口增加高度以显示整个消息。使用标签的Anchor 和MaximumSize 属性以及表单的AutoSize 属性,我能够让标签扩展到最大宽度,然后垂直扩展直到显示消息。将按钮的Anchor 属性设置为Bottom, Right 会按预期将按钮锁定在右下角,但表单仅扩展得足以显示标签,而按钮也不够。所以按钮显示在标签后面,看不到:

我想知道如何拉伸表单以完全包含标签的文本,并且仍然在底部为锚定按钮留出空间以产生如下内容:

【问题讨论】:

  • 为此制作了一个 TableLayoutPanel

标签: vb.net winforms button label


【解决方案1】:

最简单的方法是使用 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);
        }
    }
}

结果:

【讨论】:

    【解决方案2】:

    将 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-12
      • 2019-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-03
      • 2022-12-29
      相关资源
      最近更新 更多