学习.NET Winform开发 - 布局学习笔记

学习.NET Winform开发 - 布局学习笔记

创建日期:2016/04/25
更新日期:2016/04/25
发布地址:http://www.cnblogs.com/gibbonnet/p/5431418.html

任务场景

基础

  • 四行表格布局(菜单、工具栏、操作区、状态栏)
  • 第1、2、4行固定高度,中间随窗口改变而改变。

高阶

  • 中间操作区分为两列,可以改变宽度

DemoTableLayout.cs

命名空间:System.Windows.Forms

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

/// 样式表格布局
class DemoTableLayout: Form
{
    public DemoTableLayout()
    {
        this.Text = "窗口的标题";
        this.StartPosition = FormStartPosition.CenterScreen;
        this.Size = new Size(500, 600);

        TableLayoutPanel demoLayoutPanel = new TableLayoutPanel();
        
        // 使布局面板填充整个Form
        demoLayoutPanel.Dock = DockStyle.Fill;
        this.Controls.Add(demoLayoutPanel);

        // 布局方式:第一、三行随子元素,第二行填充剩余位置
        demoLayoutPanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single; 
        demoLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
        demoLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 10)); // 此处设置10或100都一样
        demoLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));

        // 添加子元素
        for(int i=0; i<3; i++) {
            Button btn = new Button();
            btn.Text = "Button";
            demoLayoutPanel.Controls.Add(btn);
            demoLayoutPanel.SetRow(btn, i);
            demoLayoutPanel.SetColumn(btn, 0);
            // 使子元素填充行
            btn.Dock = DockStyle.Fill;
        }

    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new DemoTableLayout());
    }

}

类 TableLayout

Represents a panel that dynamically lays out its contents in a grid composed of rows and columns.

  • 列数 ColumnCount
  • 行数 RowCount
  • 填充到窗口 Dock
  • 行的高度 RowStyles

枚举 DockStyle

指定控件停靠的位置和方式

{Bottom, Fill, Left, None, Right, Top}

布局的自身高度

AutoSize = true

true if the control automatically resizes based on its contents; otherwise, false. The default is true.

行的高度 RowStyle

  • 行高固定:SizeType.Absolute
  • 行高填充:SizeType.Percent?
  • 行高随子元素: SizeType.AutoSize?

SizeType {Absolute, Percent, AutoSize}

单元格边框 CellBorderStyle

TableLayoutPanelCellBorderStyle {Inset, InsetDouble, None, Outset, OutsetDouble, OutsetPartial, Single}

DemoSplitLayout.cs

转载于:https://www.cnblogs.com/gibbonnet/p/5431418.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值