渲染座位计划的最佳方式

时间:2012-07-10 21:35:46

标签: c# winforms

我正在尝试在winforms应用中动态呈现座位计划。我不确定处理这个问题的最佳方法。

我最初是用这个

开始的
        string[] blocks = new string[6] { "A", "B", "C", "D", "E", "F" };
        int[] rows = new int[6] { 10, 6, 6, 10, 8, 8 };
        int[] seats = new int[6] { 15, 10, 10, 15, 25, 25 };

忘记它只是循环遍历行和座位数组的数组大小。实际上,我需要每行渲染出不同数量的席位,每个块的行数不同。

所以在上面的代码示例中; A座有10排,每排有15个座位。 B座有6排,每排有10个座位等等

我的代码为每个席位提供了一个标签控件。

1 个答案:

答案 0 :(得分:3)

嗯,首先你应该创建一个结构:

public struct Block
{
 public string Name { get; set; }
 public int Rows { get; set; }
 public int Seats { get; set; }
}

第二次将数据填充为List:

List<Block> blocks = new List<Block>
{
 new Block { Name = "A", Rows = 10, Seats = 15 },
 new Block { Name = "B", Rows =  6, Seats = 10 },
 new Block { Name = "C", Rows =  6, Seats = 10 },
 new Block { Name = "D", Rows = 10, Seats = 15 },
 new Block { Name = "E", Rows =  8, Seats = 25 },
 new Block { Name = "F", Rows =  8, Seats = 25 },
};

并绘制或创建表单控件,廉价标签为例:

int selectedIndex = 3;

Block block = blocks[selectedIndex];

this.Text = "Block: " + block.Name; // Window Title = "Block: D"

for (int y = 0; y < block.Rows; y++)
{
 for (int x = 0; x < block.Seats; x++)
 {
  Label label = new Label();
  label.Left = x * 50;
  label.Top = y * 20;
  label.Width = 50;
  label.Height = 20;
  label.Text = "[" + (y + 1) + ", " + (x + 1) + "]";
  this.Controls.Add(label);
 }
}

为了更好的答案,请提出更准确的问题;-)