座位保留软件:立即在C#中绘制大量座位

我正在使用C#构建座位保留软件,我很困惑我如何立即抽出大量座位。 我正在尝试三种方式……

  1. 使用Usercontrol

https://dl.dropboxusercontent.com/u/81727566/seatForm.png

public void DrawUsercontrol(int x, int y) { int space = 4; int SeatLimit = 165; int RowSeatLimit = 15; for (var i = 1; i < SeatLimit; i++) { UserControl1 ctrl = new UserControl1(); ctrl.Size = new System.Drawing.Size(25, 25); ctrl.Location = new Point(x + space, y); if (i % RowSeatLimit == 0) { x = 1; y = y + 25 + space; } x = x + 25 + space; ctrl.label1.Text = i.ToString(); ctrl.label1.Click += new EventHandler(label1_Click); panel1.Controls.Add(ctrl); } } 
  1. 使用“面板”控件

     public void DrawingPanel(int x, int y) { Panel myPanel = new Panel(); int width = 16; int height = 16; myPanel.Size = new Size(width, height); myPanel.BackColor = Color.White; myPanel.Location = new Point(x, y); Label mylabel = new Label(); mylabel.Text = "4"; myPanel.Controls.Add(mylabel); myPanel.BackColor = Color.YellowGreen; // this.Controls.Add(myPanel); panel1.Controls.Add(myPanel); } 
  2. 使用Graphics并绘制Rectangle

     public void DrawingSquares(int x, int y) { SolidBrush myBrush = new SolidBrush(System.Drawing.Color.Red); Graphics graphicsObj; graphicsObj = this.panel1.CreateGraphics(); Rectangle myRectangle = new Rectangle(x, y, 30, 30); graphicsObj.FillRectangle(myBrush, myRectangle); graphicsObj.Dispose(); } 

我推荐第一个选项,但它太慢了。 我该如何决定?

您的问题是您一次只添加一个控件。 添加控件会强制父面板(最佳情况)的完全刷新( 软件 GDI +渲染非常慢),也许整个表单(最坏情况)。

尝试使用Panel.Controls.AddRange创建所有控件并将它们添加到一行中。 这只会提示一次刷新。

您还应该只在首次显示表单时以及当座位数更改时添加这些控件 – 这是一项昂贵(且相对较慢)的操作。

考虑为每个座位创建一个UserControl ,这样您就不必单独管理座位标签座位边框 – 这样您就可以只有一个列表。 如果按顺序添加座位,列表中项目的索引将映射到其座位号! 您可能不会从中获得性能提升,但您的代码将更容易使用。