图片附件: 游客没有浏览图片的权限,请
登录 或
注册
一、新建一个类库名曰“UserControls”,在里面添加两个用户控件:
1、Block,代表一个块,根据你发的图来看,每一块基本分三部分:标题、数据名和数据,那么在Block中依次放一个Label来显示标题,放置一个分隔线,放一个TableLayoutPanel来显示数据表,TableLayoutPanel初始为三行一列,第0行用来放置数据名称、第1行放置分隔线、第2行放置数据。至于列则根据设置Columns属性的值在运行时生成。Block的颜色请设置BackColor;
2、BlockLine,代表一个块行,设置Blocks属性的值来定义这行里面的一堆N个Block,就一个FlowLayoutPanel来管理布局;
二、在FormMain里引用UserControls库,你就可以根据你的业务逻辑生成N个BlockLine,这样就和TI那个差不多了;
三、有些块是没有数据名称的,这样的话你可以对Block进行修改,增加一个ColumnVisible属性并根据这个属性决定是否显示数据名称就好了,但是要注意这时列的数量就要由Values的值的所包含的数据数量来决定了,要修改代码;
四、还有一点,这里使用现有的控件组成用户控件来达到需求的,如果想进一步控制Block的外观及其它一些细节,那么建议Block做成自定义控件的形式自己绘制控件;
五、这个例子在FormMain里面放了3个BlockLine,各有3、5、2个Block,颜色不同,各部分代码如下:
程序代码:
//Block.Designer.cs
namespace UserControls
{
partial class Block
{
private components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null)) components.Dispose();
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.LabName = new System.Windows.Forms.Label();
this.Tlp = new System.Windows.Forms.TableLayoutPanel();
this.panel1 = new System.Windows.Forms.Panel();
this.panel2 = new System.Windows.Forms.Panel();
this.Tlp.SuspendLayout();
this.SuspendLayout();
this.LabName.Dock = System.Windows.Forms.DockStyle.Top;
this.LabName.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte) (134)));
this.LabName.Location = new System.Drawing.Point(0, 0);
this.LabName.Name = "LabName";
this.LabName.Size = new System.Drawing.Size(8, 15);
this.LabName.TabIndex = 0;
this.LabName.Text = "[Name]";
this.LabName.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.Tlp.AutoSize = true;
this.Tlp.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.Tlp.ColumnCount = 1;
this.Tlp.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
this.Tlp.Controls.Add(this.panel2, 0, 1);
this.Tlp.Dock = System.Windows.Forms.DockStyle.Fill;
this.Tlp.Location = new System.Drawing.Point(0, 15);
this.Tlp.Name = "Tlp";
this.Tlp.RowCount = 3;
this.Tlp.RowStyles.Add(new System.Windows.Forms.RowStyle());
this.Tlp.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 2F));
this.Tlp.RowStyles.Add(new System.Windows.Forms.RowStyle());
this.Tlp.Size = new System.Drawing.Size(8, 2);
this.Tlp.TabIndex = 1;
this.panel1.AutoSize = true;
this.panel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
this.panel1.Location = new System.Drawing.Point(0, 15);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(8, 2);
this.panel1.TabIndex = 2;
this.panel2.AutoSize = true;
this.panel2.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.Tlp.SetColumnSpan(this.panel2, 3);
this.panel2.Dock = System.Windows.Forms.DockStyle.Top;
this.panel2.Location = new System.Drawing.Point(3, 3);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(2, 1);
this.panel2.TabIndex = 3;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSize = true;
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.BackColor = System.Drawing.Color.Khaki;
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.Controls.Add(this.panel1);
this.Controls.Add(this.Tlp);
this.Controls.Add(this.LabName);
this.Name = "Block";
this.Size = new System.Drawing.Size(8, 17);
this.Tlp.ResumeLayout(false);
this.Tlp.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.Label LabName;
private System.Windows.Forms.TableLayoutPanel Tlp;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Panel panel1;
}
}
//Block.cs
using System.Collections.Generic;
using using System.Globalization;
using System.Windows.Forms;
namespace UserControls
{
public partial class Block : UserControl
{
#region 全局字段
private List<string> _columns;
private List<int> _values;
#endregion
#region 构造函数
public Block()
{
InitializeComponent();
}
#endregion
#region 公共属性
[Browsable(false)]
public List<string> Columns
{
get { return _columns; }
set
{
if (value == null || value.Count == 0) return;
_columns = value;
Tlp.ColumnCount = _columns.Count;
Tlp.Controls.Clear();
var panelSeparator = new Panel
{
BorderStyle = BorderStyle.FixedSingle,
AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowAndShrink,
Height = 1,
Dock = DockStyle.Top,
Margin = new Padding(0)
};
Tlp.Controls.Add(panelSeparator, 0, 1);
Tlp.SetColumnSpan(panelSeparator, _columns.Count);
Tlp.ColumnStyles.Clear();
for (var i = 0; i < _columns.Count; i++)
{
Tlp.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
var labelColumnName = new Label
{
Text = _columns[i],
AutoSize = true,
Margin = new Padding(3, 3, 0, 0)
};
Tlp.Controls.Add(labelColumnName, i, 0);
var labelValue = new Label
{
Text = "0",
AutoSize = true,
Margin = new Padding(3, 0, 0, 0)
};
Tlp.Controls.Add(labelValue, i, 2);
}
}
}
public string Title
{
get { return LabName.Text; }
set { LabName.Text = value; }
}
public List<int> Values
{
get { return _values; }
set
{
if (value == null || value.Count == 0) return;
_values = value;
for (var i = 0; i < _columns.Count; i++)
{
var labelValue = (Label)Tlp.Controls[(i + 1) * 2];
labelValue.Text = _values[i].ToString(CultureInfo.InvariantCulture);
}
}
}
#endregion
}
}
//BlockLine.Designer.cs
namespace UserControls
{
partial class BlockLine
{
private components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null)) components.Dispose();
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.Flp = new System.Windows.Forms.FlowLayoutPanel();
this.SuspendLayout();
this.Flp.AutoSize = true;
this.Flp.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.Flp.Dock = System.Windows.Forms.DockStyle.Fill;
this.Flp.Location = new System.Drawing.Point(0, 0);
this.Flp.Name = "Flp";
this.Flp.Size = new System.Drawing.Size(0, 0);
this.Flp.TabIndex = 0;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSize = true;
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.Controls.Add(this.Flp);
this.Name = "BlockLine";
this.Size = new System.Drawing.Size(0, 0);
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.FlowLayoutPanel Flp;
}
}
//BlockLine.cs
using System.Collections.Generic;
using System.Windows.Forms;
namespace UserControls
{
public partial class BlockLine : UserControl
{
#region 全局字段
private List<Block> _blocks;
#endregion
#region 构造函数
public BlockLine()
{
InitializeComponent();
}
#endregion
#region 公共属性
public List<Block> Blocks
{
get { return _blocks; }
set
{
if (value == null || value.Count == 0) return;
_blocks = value;
Flp.Controls.Clear();
foreach (var block in _blocks)
{
block.Margin = new Padding(0, 2, 2, 0);
Flp.Controls.Add(block);
}
}
}
#endregion
}
}
//FormMain.Designer.cs
namespace LikeTI
{
partial class FormMain
{
private components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null)) components.Dispose();
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.blockLine2 = new UserControls.BlockLine();
this.blockLine1 = new UserControls.BlockLine();
this.blockLine3 = new UserControls.BlockLine();
this.SuspendLayout();
this.blockLine2.AutoSize = true;
this.blockLine2.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.blockLine2.Blocks = null;
this.blockLine2.Dock = System.Windows.Forms.DockStyle.Top;
this.blockLine2.Location = new System.Drawing.Point(0, 0);
this.blockLine2.Name = "blockLine2";
this.blockLine2.Size = new System.Drawing.Size(647, 0);
this.blockLine2.TabIndex = 1;
this.blockLine1.AutoSize = true;
this.blockLine1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.blockLine1.Blocks = null;
this.blockLine1.Dock = System.Windows.Forms.DockStyle.Top;
this.blockLine1.Location = new System.Drawing.Point(0, 0);
this.blockLine1.Name = "blockLine1";
this.blockLine1.Size = new System.Drawing.Size(647, 0);
this.blockLine1.TabIndex = 0;
this.blockLine3.AutoSize = true;
this.blockLine3.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.blockLine3.Blocks = null;
this.blockLine3.Dock = System.Windows.Forms.DockStyle.Top;
this.blockLine3.Location = new System.Drawing.Point(0, 0);
this.blockLine3.Name = "blockLine3";
this.blockLine3.Size = new System.Drawing.Size(647, 0);
this.blockLine3.TabIndex = 2;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(647, 262);
this.Controls.Add(this.blockLine3);
this.Controls.Add(this.blockLine2);
this.Controls.Add(this.blockLine1);
this.Name = "FormMain";
this.ShowIcon = false;
this.Text = "Like TI by ";
this.ResumeLayout(false);
this.PerformLayout();
}
private UserControls.BlockLine blockLine1;
private UserControls.BlockLine blockLine2;
private UserControls.BlockLine blockLine3;
}
}
//FormMain.cs
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using UserControls;
namespace LikeTI
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
blockLine1.Blocks = new List<Block>
{
new Block
{
Title = "Block00",
Columns = new List<string> {"C1", "C2", "C3"},
BackColor = Color.Coral
},
new Block
{
Title = "Block01",
Columns = new List<string> {"C1", "C2", "C3"},
BackColor = Color.Coral
},
new Block
{
Title = "Block02",
Columns = new List<string> {"C1", "C2", "C3"},
BackColor = Color.Coral
}
};
blockLine2.Blocks = new List<Block>
{
new Block
{
Title = "Block10",
Columns = new List<string> {"C1", "C2", "C3"},
Values = new List<int> {0, 1, 2}
},
new Block
{
Title = "Block11",
Columns = new List<string> {"C1", "C2", "C3"},
Values = new List<int> {3, 4, 5}
},
new Block
{
Title = "Block12",
Columns = new List<string> {"C1", "C2", "C3"},
Values = new List<int> {6, 7, 8}
},
new Block
{
Title = "Block13",
Columns = new List<string> {"C1", "C2", "C3"},
Values = new List<int> {9, 10, 11}
},
new Block
{
Title = "Block14",
Columns = new List<string> {"C1", "C2", "C3"},
Values = new List<int> {12, 13, 14}
}
};
blockLine3.Blocks = new List<Block>
{
new Block
{
Title = "Block10",
Columns = new List<string> {"C1", "C2", "C3"},
Values = new List<int> {0, 1, 2},
BackColor = Color.Gainsboro
},
new Block
{
Title = "Block11",
Columns = new List<string> {"C1", "C2", "C3"},
Values = new List<int> {3, 4, 5},
BackColor = Color.Gainsboro
}
};
}
}
}
[
本帖最后由 mmxo 于 2012-11-12 15:32 编辑 ]