M
michael sorens
I am trying to achieve a simple visual effect in a WinForm application, .NET
2.0, but I am missing something. The sample code below contains just a button
and a FlowLayoutPanel. Pressing the button should add a new label into the
FlowLayoutPanel.
Key points about the layout:
--The FlowLayoutPanel is anchored on all sides so that if the user resizes
the form, the panel resizes with it.
--The FlowLayoutPanel has AutoSize set true so that as it gets more contents
it will expand.
--Similarly, the containing Form has AutoSize set true.
--The FlowLayoutPanel direction is set to TopDown with the intention that
added items will grow downward, rather than to the right.
Expected results:
--Each added label will have its full text shown on one line.
--Each added label will be on a line by itself.
--Each new added label will be on the line below the previously added label.
--The form will expand downward (vertically) to accommodate the new labels
as needed.
Actual results:
--Each new item is added left to right, not top to bottom.
--The FlowLayoutPanel and the form expand, but again, to the right.
--The items when added are not added at full width, but wrap to multiple
lines.
--With or without setting FlowBreak, the results are unchanged.
Here is the sample code for the Winform, containing merged code from
Form1.cs and Form1.Designer.cs for your convenience (you will still need to
invoke it from the boilerplate Program.cs):
================================
using System;
using System.Windows.Forms;
namespace TestFlow
{
public partial class Form1 : Form
{
private System.ComponentModel.IContainer components = null;
private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1;
private System.Windows.Forms.Button button1;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// flowLayoutPanel1
//
this.flowLayoutPanel1.Anchor =
((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top
| System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.flowLayoutPanel1.AutoSize = true;
this.flowLayoutPanel1.FlowDirection =
System.Windows.Forms.FlowDirection.TopDown;
this.flowLayoutPanel1.Location = new System.Drawing.Point(12, 25);
this.flowLayoutPanel1.Name = "flowLayoutPanel1";
this.flowLayoutPanel1.Size = new System.Drawing.Size(225, 120);
this.flowLayoutPanel1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(27, 0);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 19);
this.button1.TabIndex = 2;
this.button1.Text = "Add One";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// TestForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSize = true;
this.ClientSize = new System.Drawing.Size(263, 148);
this.Controls.Add(this.button1);
this.Controls.Add(this.flowLayoutPanel1);
this.Name = "TestForm";
this.Text = "TestForm";
this.ResumeLayout(false);
this.PerformLayout();
}
public Form1()
{
InitializeComponent();
}
int count = 0;
private void button1_Click(object sender, EventArgs e)
{
Label l = new Label();
l.Text = "-Dummy message here with count = " + count++;
l.AutoSize = true;
flowLayoutPanel1.Controls.Add(l);
flowLayoutPanel1.SetFlowBreak(l, true);
}
}
}
================================
2.0, but I am missing something. The sample code below contains just a button
and a FlowLayoutPanel. Pressing the button should add a new label into the
FlowLayoutPanel.
Key points about the layout:
--The FlowLayoutPanel is anchored on all sides so that if the user resizes
the form, the panel resizes with it.
--The FlowLayoutPanel has AutoSize set true so that as it gets more contents
it will expand.
--Similarly, the containing Form has AutoSize set true.
--The FlowLayoutPanel direction is set to TopDown with the intention that
added items will grow downward, rather than to the right.
Expected results:
--Each added label will have its full text shown on one line.
--Each added label will be on a line by itself.
--Each new added label will be on the line below the previously added label.
--The form will expand downward (vertically) to accommodate the new labels
as needed.
Actual results:
--Each new item is added left to right, not top to bottom.
--The FlowLayoutPanel and the form expand, but again, to the right.
--The items when added are not added at full width, but wrap to multiple
lines.
--With or without setting FlowBreak, the results are unchanged.
Here is the sample code for the Winform, containing merged code from
Form1.cs and Form1.Designer.cs for your convenience (you will still need to
invoke it from the boilerplate Program.cs):
================================
using System;
using System.Windows.Forms;
namespace TestFlow
{
public partial class Form1 : Form
{
private System.ComponentModel.IContainer components = null;
private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1;
private System.Windows.Forms.Button button1;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// flowLayoutPanel1
//
this.flowLayoutPanel1.Anchor =
((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top
| System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.flowLayoutPanel1.AutoSize = true;
this.flowLayoutPanel1.FlowDirection =
System.Windows.Forms.FlowDirection.TopDown;
this.flowLayoutPanel1.Location = new System.Drawing.Point(12, 25);
this.flowLayoutPanel1.Name = "flowLayoutPanel1";
this.flowLayoutPanel1.Size = new System.Drawing.Size(225, 120);
this.flowLayoutPanel1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(27, 0);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 19);
this.button1.TabIndex = 2;
this.button1.Text = "Add One";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// TestForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSize = true;
this.ClientSize = new System.Drawing.Size(263, 148);
this.Controls.Add(this.button1);
this.Controls.Add(this.flowLayoutPanel1);
this.Name = "TestForm";
this.Text = "TestForm";
this.ResumeLayout(false);
this.PerformLayout();
}
public Form1()
{
InitializeComponent();
}
int count = 0;
private void button1_Click(object sender, EventArgs e)
{
Label l = new Label();
l.Text = "-Dummy message here with count = " + count++;
l.AutoSize = true;
flowLayoutPanel1.Controls.Add(l);
flowLayoutPanel1.SetFlowBreak(l, true);
}
}
}
================================