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);
}
}
}
================================
Peter Duniho - 24 Mar 2008 18:16 GMT
> [...]
> --The FlowLayoutPanel direction is set to TopDown with the intention that
> added items will grow downward, rather than to the right.
I don't think that property does what you think it does.
> Expected results:
> --Each added label will have its full text shown on one line.
[quoted text clipped - 11 lines]
> lines.
> --With or without setting FlowBreak, the results are unchanged.
Granted, I haven't used this control much and the documentation for it is
remarkably vague. However, given that the default behavior would be to
flow new controls into the panel in a left-to-right, top-to-bottom order,
and given that you've explicitly told it to do top-to-bottom first, then
left-to-right _and_ you have also set each new control as a break-point
within the flow, it's not at all surprising to me that the controls are
added left to right only.
I'd recommend first trying the same code except without changing the
default value for FlowDirection. That may not do exactly what you want,
but it should be a lot closer. Once you've got the controls correctly
filling in just one per line, then you can fiddle with the other settings
to get them added in precisely the way you want.
Pete
michael sorens - 24 Mar 2008 18:50 GMT
I thought that in the course of experimentation I had not found a good
combination of settings, which is why I eneded up setting both FlowDirection
and FlowBreak. From your comment I had an "aha!" moment and realized that
yes, as you said, the FlowBreak was interacting with the FlowDirection to
give me left-to-right behavior. I found that either FlowDirection or
FlowBreak will individually give me what I want. Thanks.
pavan2004 - 25 Mar 2008 05:31 GMT
Hi michael sorens ,
By seeing your post i came to know that you are working on winforms
application development. May i know difference between "Windows based
application and winform application "
michael sorens - 25 Mar 2008 16:03 GMT
Those two terms could be synonymous, depending upon your context. Generally,
"winform" application means a program running on your desktop whereas
"webform" application means a program running in your browser.
Ben Voigt [C++ MVP] - 26 Mar 2008 20:55 GMT
> Those two terms could be synonymous, depending upon your context.
> Generally, "winform" application means a program running on your
> desktop whereas "webform" application means a program running in your
> browser.
Web applications would usually not be called Windows application
development.
But .NET now has two toolkits for making graphical Windows applications:
Windows.Forms and WPF.