1

我正在尝试在 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个座位,依此类推

我的代码为每个座位呈现了一个标签控件。

4

1 回答 1

3

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

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

第二次将数据填充为列表:

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);
 }
}

要获得更好的答案,请提出更精确的问题;-)

于 2012-07-10T22:00:44.100 回答