C#中如何用循环填充文本按钮对应的每个座位号?

How to fill every seat number corresponding to the text button with a loop in C#?

首先,我是编程菜鸟,我不会英文,如果表达有误,请见谅:D

我正在做一个代表预订系统巴士的程序。我在运行时用按钮数组创建了 64 个按钮,但这不是问题所在。
问题是表示与公交车座位数对应的每个文本按钮。这是我的代码:

var buttonArray = new Button[64];

for (var i = 1; i <= 4; i++)
{
   buttonArray[i] = new Button();
   buttonArray[i].Size = new Size(75, 23);
   buttonArray[i].Name = "button" + i;
   buttonArray[i].Text = "Seat " + i;
   buttonArray[i].Location = new Point(-50 + (i*100), 10);

   panel1.Controls.Add(buttonArray[i]);

   for (var j = 5; j <= 19; j++)
   {
        buttonArray[j] = new Button();
        buttonArray[j].Size = new Size(75, 23);
        buttonArray[j].Name = "button" + j;
        buttonArray[j].Text = "Seat " + j;
        buttonArray[j].Location = new Point(-50 + (i * 100), -105 + (j * 30));

        panel1.Controls.Add(buttonArray[j]);
    }
}

结果将是用 4 列和 16 行表示的 64 个按钮,例如:

这应该可以满足您的需求,

Point pt;
int y = 20;
int x = 0;
for (int i = 0; i < 16; i++)
{
   x = 20;
   for (int j = 1; j <= 4; j++)
   {
      pt = new Point(x, y);
      Button btn = new Button() { Location = pt, Size = new Size(75, 23) };
      btn.Name = i.ToString() + j.ToString();
      btn.Text = string.Format("{0} {1:D2}", "Seat", i * 4 + j);
      panel1.Controls.Add(btn);
      x += 90;
    }
    y += 30;
 }

这是我的做法:只需从 0 循环到 63,然后即时计算行和列。然后设置按钮属性并将其添加到面板。您可以通过调整 seatWidthseatHeight 变量来更改座位的大小。另请注意,我在前两列和后两列之间添加了一些额外的 space,因为看起来这就是您在图表中的内容。您可以使用 middleRowWidth 变量进行调整。您还可以通过调整 seatSpacing 值来自定义座位之间的距离:

const int seatSpacing = 4;
const int middleRowWidth = 20;
const int seatWidth = 55;
const int seatHeight = 20;
panel1.Width = 4 * (seatWidth + seatSpacing) + seatSpacing + middleRowWidth;

var buttonSize = new Size(seatWidth, seatHeight);

for (var i = 0; i < 64; i++)
{
    // Calculate the location for this seat
    int thisRow = i / 4;
    int thisColumn = i % 4;

    int seatTop = thisRow * (seatHeight + seatSpacing);
    int seatLeft = thisColumn * (seatWidth + seatSpacing);

    // Add some extra distance down the middle
    if (thisColumn >= 2) seatLeft += middleRowWidth;

    // Create a new button
    var thisButton = new Button
    {
        Size = buttonSize,
        Name = "button" + (i + 1),
        Text = "Seat " + (i + 1),
        Location = new Point(seatLeft, seatTop),
        Visible = true,
    };

    // Add it to the panel
    panel1.Controls.Add(thisButton);
}

结果:

更新

在考虑如何使它更通用之后,我想到了一些飞机的座位布局。有些每边有两个座位,有些有三个,有些每边有两个,中间有一排三个。为了适应不同类型的布局,我稍微修改了代码。下面的代码为您的布局提供了可自定义的变量。下面的是 2-3-2 布局,其中数字是座位数,- 是过道。它还包括两个 "exit row" 水平通道 运行:

// Seating Layout
const int numberOfRows = 20;
const int numberOfColumns = 7;
const int numberOfSeats = numberOfRows * numberOfColumns;
var columnsToInsertAisleAfter = new List<int> {1, 4};
var rowsToInsertAisleAfter = new List<int> {6, 12};

// Seat sizing
const int seatWidth = 35;
const int seatHeight = 20;
const int seatSpacing = 4;
const int aisleWidth = 20;
var seatSize = new Size(seatWidth, seatHeight);

// Panel and form layout
panel1.Size = new Size(
    numberOfColumns * (seatWidth + seatSpacing) + seatSpacing +
    aisleWidth * columnsToInsertAisleAfter.Count,
    numberOfRows * (seatHeight + seatSpacing) + seatSpacing +
    aisleWidth * rowsToInsertAisleAfter.Count);

this.Size =
    new Size(panel1.Size.Width + (panel1.Left * 2),
        panel1.Size.Height + (panel1.Top * 2)) +
    new Size(this.Width - this.ClientSize.Width,
        this.Height - this.ClientSize.Height);

// Add seats to panel
for (var i = 0; i < numberOfSeats; i++)
{
    // Calculate the location for this seat
    int thisRow = i / numberOfColumns;
    int thisColumn = i % numberOfColumns;

    int seatTop = thisRow * (seatHeight + seatSpacing);
    int seatLeft = thisColumn * (seatWidth + seatSpacing);

    // Add some extra distance for aisles
    var aislesBeforeThisColumn = 
        columnsToInsertAisleAfter.Count(col => col < thisColumn);
    seatLeft += aislesBeforeThisColumn * aisleWidth;

    var aislesBeforeThisRow = 
        rowsToInsertAisleAfter.Count(row => row < thisRow);
    seatTop += aislesBeforeThisRow * aisleWidth;

    // Add a new button to the panel
    panel1.Controls.Add(new Button
    {
        Size = seatSize,
        Name = "button" + (i + 1),
        Text = (i + 1).ToString(),
        Location = new Point(seatLeft, seatTop),
        Visible = true,
    });
}

大飞机布局: