[转]WinForm"tableLayoutPanel和flowLayoutPanel的使用(附源码示例)"

这篇文章主要跟大家分享下,在配餐系统的开发中,对tableLayoutPanel 和 flowLayoutPanel 控件的使用方法和技巧 ——后附上 测试demo, 相信需要的朋友下载看后能很快的知道其如何使用并实现一些效果和扩展应用!

        tableLayoutPanel: 表格布局面板,适合以表格形式规则的动态添加(显示)控件。使用方法概述:1.将 tableLayoutPanel 拖放到窗体指定区域 ——一般做些基本的设置 或添加行或列,其它的就需写代码来控制显示 2.代码:

a.

 
  
// 删除默认的行和列样式
tableLayoutPanel4.ColumnStyles.Clear();
tableLayoutPanel4.RowStyles.Clear();

——删除默认的行和列样式,避免影响表格整体显示的效果,使样式以默认或自定义的为准(注:tableLayoutPanel 拖放到窗体,默认为两行两列, 默认添加的代码如下:

 
  
this .tableLayoutPanel5.ColumnCount = 2 ;
this .tableLayoutPanel5.ColumnStyles.Add( new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this .tableLayoutPanel5.ColumnStyles.Add( new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this .tableLayoutPanel5.Location = new System.Drawing.Point( 72 , 82 );
this .tableLayoutPanel5.Name = " tableLayoutPanel5 " ;
this .tableLayoutPanel5.RowCount = 2 ;
this .tableLayoutPanel5.RowStyles.Add( new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this .tableLayoutPanel5.RowStyles.Add( new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));

b. 设置属性和添加控件到单元格



 
  
tableLayoutPanel4.AutoScroll = true ;
tableLayoutPanel4.BackColor = Color.White;
tableLayoutPanel4.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
tableLayoutPanel4.ColumnCount = 6 ;
for ( int i = 0 ; i < 26 ; i ++ )
{
// ——行和列的 样式 默认为auto
// tableLayoutPanel4.ColumnStyles.Add(new ColumnStyle());
// tableLayoutPanel4.RowStyles.Add(new RowStyle());
Label lab = new Label();
lab.Text = " Label_ " + i;
lab.AutoSize = true ;
// 通过Anchor 设置Label 列中居中
lab.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
tableLayoutPanel4.Controls.Add(lab);

TextBox txtObj = new TextBox();
txtObj.Text = " TextBox_ " + i;
txtObj.Width = 70 ;
tableLayoutPanel4.Controls.Add(txtObj);
}

ok, 以上代码即可实现,6列 行数根据添加项(上面的 lab和txtObj可称之为单元格的项)的个数多少而自动转行 tableLayoutPanel4.Controls.Add()方法会将控件添加到当前行单元格的后面,如果是最后一列,则自动转到下一行。

测试效果:2011021823120048.gif



 flowLayoutPanel: 流式布局面板,直接看代码吧,就不多说了.



[tableLayoutPanel 和 flowLayoutPanel 控件的区别:tableLayoutPanel是根据是否是最后一列而转到下一行,更容易控制布局,适合做‘扫雷’、'五子棋'等网格类的布局显示控件;而flowLayoutPanel是根据当前行余下的宽度不足以放接下来的控件而转到下一行,如 下图,适用于不需要过多考虑布局的显示。]

 2011021823035958.gif想了解它们更多的应用,可浏览:http://msdn.microsoft.com/zh-cn/library/system.windows.forms.tablelayoutpanel(VS.80).aspx

    最后,补充说明一下,我也是第一次对这两个控件有比较实际的应用,对其只能算是有个大概的了解,有不对的地方,希望大家能给予指正,避免'误人子弟',呵呵...

    

源码示例下载

转摘自:http://www.cnblogs.com/know/archive/2011/02/18/1958260.html

转载于:https://www.cnblogs.com/hxworm/articles/1959544.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TableLayoutPanelFlowLayoutPanel都是WinForm中常用的布局控件TableLayoutPanel是一个表格布局控件,可以将控件以行列的形式排列,每个单元格可以放置一个控件控件大小可以自由调整。 使用方法: 1. 在Visual Studio的工具箱中找到TableLayoutPanel控件并拖拽到窗体中。 2. 设置TableLayoutPanel的行列数和单元格大小。 3. 在TableLayoutPanel中添加控件使用Add方法指定控件所在的行列。 示例代码: ``` //创建TableLayoutPanel TableLayoutPanel tableLayoutPanel1 = new TableLayoutPanel(); tableLayoutPanel1.RowCount = 2; tableLayoutPanel1.ColumnCount = 2; tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single; tableLayoutPanel1.Dock = DockStyle.Fill; this.Controls.Add(tableLayoutPanel1); //添加控件 Button button1 = new Button(); button1.Text = "Button1"; tableLayoutPanel1.Controls.Add(button1, 0, 0); Button button2 = new Button(); button2.Text = "Button2"; tableLayoutPanel1.Controls.Add(button2, 0, 1); Button button3 = new Button(); button3.Text = "Button3"; tableLayoutPanel1.Controls.Add(button3, 1, 0); Button button4 = new Button(); button4.Text = "Button4"; tableLayoutPanel1.Controls.Add(button4, 1, 1); ``` FlowLayoutPanel是一个流式布局控件,可以将控件按照添加的顺序自动排列,并且可以自动换行。 使用方法: 1. 在Visual Studio的工具箱中找到FlowLayoutPanel控件并拖拽到窗体中。 2. 添加控件FlowLayoutPanel中。 示例代码: ``` //创建FlowLayoutPanel FlowLayoutPanel flowLayoutPanel1 = new FlowLayoutPanel(); flowLayoutPanel1.Dock = DockStyle.Fill; this.Controls.Add(flowLayoutPanel1); //添加控件 Button button1 = new Button(); button1.Text = "Button1"; flowLayoutPanel1.Controls.Add(button1); Button button2 = new Button(); button2.Text = "Button2"; flowLayoutPanel1.Controls.Add(button2); Button button3 = new Button(); button3.Text = "Button3"; flowLayoutPanel1.Controls.Add(button3); Button button4 = new Button(); button4.Text = "Button4"; flowLayoutPanel1.Controls.Add(button4); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值