How to get FlowLayoutPanel.AutoSize to work with FlowBreak

have a problem with a FlowLayoutPanel and I don't know how to solve it.

I'm placing two FlowLayoutPanels inside another; the second inner flp has 3 buttons inside.

How to get FlowLayoutPanel.AutoSize to work with FlowBreak

The properties from FlowLayoutPanel child are:

FlowDirection=LeftToRight;AutoSize=true;AutoSizeMode=GrowAndShrink;WrapContents=true;

Now I set for each button the FlowBreak property to true, however the behavior I see is not the one I want, I want the FlowLayoutPanel to shrink to the width of the buttons,

How to get FlowLayoutPanel.AutoSize to work with FlowBreak

Changing FlowDirection to UpToDown is not an option.

Anyone know why the AutoSize is not working?

this is the code.

////FlowLayoutPanel1//this.FlowLayoutPanel1.AutoSizeMode=System.Windows.Forms.AutoSizeMode.GrowAndShrink;this.FlowLayoutPanel1.Controls.Add(this.FlowLayoutPanel3);this.FlowLayoutPanel1.Location=newSystem.Drawing.Point(84,77);this.FlowLayoutPanel1.MinimumSize=newSystem.Drawing.Size(10,10);this.FlowLayoutPanel1.Name="FlowLayoutPanel1";this.FlowLayoutPanel1.Size=newSystem.Drawing.Size(308,265);this.FlowLayoutPanel1.TabIndex=0;////FlowLayoutPanel3//this.FlowLayoutPanel3.AutoSize=true;this.FlowLayoutPanel3.AutoSizeMode=System.Windows.Forms.AutoSizeMode.GrowAndShrink;this.FlowLayoutPanel3.Controls.Add(this.Button1);this.FlowLayoutPanel3.Controls.Add(this.Button2);this.FlowLayoutPanel3.Controls.Add(this.Button3);this.FlowLayoutPanel3.Location=newSystem.Drawing.Point(127,3);this.FlowLayoutPanel3.MinimumSize=newSystem.Drawing.Size(10,10);this.FlowLayoutPanel3.Name="FlowLayoutPanel3";this.FlowLayoutPanel3.Size=newSystem.Drawing.Size(162,87);this.FlowLayoutPanel3.TabIndex=1;////Button1//this.FlowLayoutPanel3.SetFlowBreak(this.Button1,true);this.Button1.Location=newSystem.Drawing.Point(3,3);this.Button1.Name="Button1";this.Button1.Size=newSystem.Drawing.Size(75,23);this.Button1.TabIndex=0;this.Button1.Text="Button1";this.Button1.UseVisualStyleBackColor=true;////Button2//this.FlowLayoutPanel3.SetFlowBreak(this.Button2,true);this.Button2.Location=newSystem.Drawing.Point(3,32);this.Button2.Name="Button2";this.Button2.Size=newSystem.Drawing.Size(75,23);this.Button2.TabIndex=1;this.Button2.Text="Button2";this.Button2.UseVisualStyleBackColor=true;////Button3//this.Button3.Location=newSystem.Drawing.Point(3,61);this.Button3.Name="Button3";this.Button3.Size=newSystem.Drawing.Size(75,23);this.Button3.TabIndex=2;this.Button3.Text="Button3";this.Button3.UseVisualStyleBackColor=true;
////

It is a bug, it's been around for a very long time. The issue is that the layout engine for FlowLayoutPanel calculates the width of the first row wrong, including the width of the 2nd control, even though it got wrapped to the second row.

The workaround is silly but effective, add a dummy Panel with a Width of 0 after the 1st control. If you are doing this with the designer then drop it first and drag it in the right place, to the right of the 1st control. Then set its Margin to (0, 0, 0, 0) and Size to (0, 0) in the Properties window.

////

5 down vote

I don't believe the FlowLayoutPanel was designed to do what you're trying to do. A TableLayoutPanel would probably be better suited. Add a TableLayoutPanel with a single column, and add each button to a row.

Edit: I found a hackish work around. After the first button, create a Panel with the size of 0,0 and the margin of 0,0. Make sure that FlowBreak is set to false.

How to get FlowLayoutPanel.AutoSize to work with FlowBreak

Edit: You only need to create one panel, after the first button, not one for each.

更多相关文章
一周排行
  • Ugly Number Total Accepted: 20760 Total Submissions: 63208 Difficulty: Easy   Write a program to check wheth ...
  • MyBatis知多少(10)應用程序數據庫
    应用程序数据库往往是最小.最简单.也最易于使用的数据库.这种数据库往往是我们这些开发人员通
  • 一. OpenVPN 安装环境 Server 端的环境 [[email protected] etc]# lsb_release -a LSB Version: :core-4.0-amd64:core-4.0-ia32: ...
  • [题目]改进猜数游戏程序,功能是:  a.允许用户反复输入数,直至猜中程序选定的数. b.输入的数如果大于选定的数,则提示“太大了”:如果小于选定的数,则提示“太小了”: 如果等于选定的数,则输出“你赢了”并结束程序
  • 


    		    Linux學習筆記之 RAID介紹,及centos6上軟RAID的實現
    RAID:Redundant Arrays of Inexpensive Disk 廉价冗
  • .NET開源工作流RoadFlowBug修改1.8.2表單驗證時ueditor編輯非空驗證無效
    RoadFlow生成的表单,Ueditor编辑器不能进行非空验证的BUG修改: 1.修改控
  • MTKAndroid Launcher分析HotSeat進階
    讲解如何在Launcher里面配置以及修改Hotseat的参数 现在讲解一下如何在Hots
  • IE9的css hack
    以前写过<IE8的css hack>,ie9一出css hack也该更新,以前 ...
  • 


    		    Spring中XML配置的12個技巧
    Spring利用依赖注入可以获得简单而有效的测试能力.Spring beans,依赖关系,
  •  题目大意: 给定n个点,给每个点都安排一个相同的正方形,使这个点落在正方形的下底边的中间或者上底边的中间,并让这n个正方形不出现相互覆盖,可以共享同一条边,求 这个正方形最大的边长   这里明显看出n个点,每个点都