1

I'm new to C# and may be missing something obvious. I've found only one related question here, and it was not exactly a match nor very authoritative.

What I want is to make it so that I can dock a control inside a FlowLayout (set left-to-right) so that it will fill the whole vertical space, and when the user resizes the Form, the controls resize to continue filling the whole vertical space. Can this be done, and if so, how?

What I've found:

I've created a Form. I've coloured it red. I've added a FlowLayout which I've coloured green and set the Dock to fill the Form. This seems to work (the Form is now all green, no red visible).

I then add a couple of controls to the FlowLayout (which is running left-to-right). If I dock them top, it works as expected. If I dock them bottom, it is the same as top; suggesting the vertical space is not really the whole green area. Docking left, right or fill the components won't appear at all unless I give them a minimum size > 0.

I have some code which shows what I mean; I have added some size to one of the controls and it can be seen that this seems to resize to the minimum vertical size allowed. The output screenshot is included.

Screenshot of Form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ViewData
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            CustomComponentLayout();
        }

        private void CustomComponentLayout()
        {
            FlowLayoutPanel pnlHorFlow;
            pnlHorFlow = new FlowLayoutPanel();
            this.Controls.Add(pnlHorFlow);
            pnlHorFlow.Dock = DockStyle.Fill;
            pnlHorFlow.BackColor = Color.Green;

            // Add a panel with some size
            Panel testPanel = new Panel();
            testPanel.Dock = DockStyle.Fill;
            testPanel.BackColor = Color.BlanchedAlmond;
            // Need these to get anything            
            testPanel.MinimumSize = new Size(200, 400);
            pnlHorFlow.Controls.Add(testPanel);

            Button testButton;
            testButton = new Button();
            testButton.Text = "Test";
            testButton.Anchor = AnchorStyles.Top & AnchorStyles.Bottom;
            testButton.Dock = DockStyle.Left;
            pnlHorFlow.Controls.Add(testButton);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

EDIT

Ok, so the answer below doesn't answer the question, but I've accepted it because I am now convinced that the answer is that FlowLayoutPanel just can't do this. The answer (at least the edit) is about as close as I'll get. Playing with TableLayoutPanel seems to be the only way to get what I need.

For those in this boat, I also found this helpful.

I've found a lot of people making the same wrong assumptions about FlowLayoutPanel as me. I think in my case it ius because I have a Java background, and FlowLayouts work differently in Java.

Community
  • 1
  • 1
timbo
  • 1,533
  • 1
  • 15
  • 26
  • The `FlowLayoutPanel` class does not use the `DockStyle` or `AnchorStyles` properties. Its main feature is its `FlowDirection` property. `TableLayoutPanel` might be better suited to the layout you are trying to create. – Loathing Oct 26 '15 at 09:01
  • I'll give it a shot, thanks. Weirdly difficult information to find. – timbo Oct 26 '15 at 10:40
  • The core documentation is not helpful. However, there are some tutorials that provide better information, e.g. https://msdn.microsoft.com/en-us/library/ms171691%28v=vs.110%29.aspx In my experience, `AnchorStyles` work relative to the other controls. So one control by itself is not enough for anchor styles to work correctly. Somewhat unintuitive. That said, it's up to the `LayoutEngine's` algorithm to decide which properties to use, and how to use them. – Loathing Oct 26 '15 at 21:07
  • So my first comment about it not using `AnchorStyles` is not true. – Loathing Oct 26 '15 at 21:12

1 Answers1

1

Here is an example of using AnchorStyles.Right:enter image description here

public class FlowForm2 : Form {

    public FlowForm2() {
        Button btn1 = new Button { Text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", AutoSize = true };
        Button btn2 = new Button { Text = "ABCD", AutoSize = true };
        btn2.Anchor = AnchorStyles.Right;

        FlowLayoutPanel p = new FlowLayoutPanel { Dock = DockStyle.Fill, FlowDirection = FlowDirection.TopDown, WrapContents = false };
        p.Controls.Add(btn1); // if btn1 isn't added, then btn2 appears on the LEFT side
        p.Controls.Add(btn2); // however, if btn1 is added, then btn2 is right justified with the right edge of btn2

        Controls.Add(p);
    }
}

EDIT:

The easiest way to create the layout you want is:

public class TLPForm : Form {

    TableLayoutPanel p = new TableLayoutPanel { Dock = DockStyle.Fill };

    public TLPForm() {
        var style = AnchorStyles.Top | AnchorStyles.Bottom;
        Button btn1 = new Button { Text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", AutoSize = true, AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink, Anchor = style };
        Button btn2 = new Button { Text = "ABCD", AutoSize = true, AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink, Anchor = style };
        Button btn3 = new Button { Text = "ABCD", AutoSize = true, AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink, Anchor = style };
        p.Controls.Add(btn1, 0, 0);
        p.Controls.Add(btn2, 1, 0);
        p.Controls.Add(btn3, 2, 0);
        p.Controls.Add(new Control(), 3, 0); // <-- takes up the rest of the space
        Controls.Add(p);
    }
}
Loathing
  • 5,109
  • 3
  • 24
  • 35
  • Thanks for the response. This is consistent with what I've found, but what I want is for the components to cling to the right hand side. The layout appears to take up the whole container it sits in, but the components always seem to be stuck packed into the top left. – timbo Oct 26 '15 at 22:03
  • Sorry, don't want components to cling to right. I mean stretch top to bottom. – timbo Oct 26 '15 at 22:03