1
votes

I have a Windows form with an image, a label that contains an error message, and a "Close" button:

Before

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:

After

I'd like to know how I can stretch the form to fully contain the text of the label and still leave room at the bottom for the anchored button to produce something like this:

Desired

2
A TableLayoutPanel is made for thisŇɏssa Pøngjǣrdenlarp

2 Answers

1
votes

Add 3 panels to your form docked left, bottom and fill. Set the properties as indicated in the image below.

Then set the Label's maximum width to a fixed value in the properties or you can calculate it at run-time:

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

example form

0
votes

The easiest way is to build the layout with 3 docked panels, like this (hope you can adjust for your needs):

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

Result:

enter image description here