创建 “座位号(i+1)” 的按钮组件,座位号从1开始
时间: 2024-02-28 21:57:02 浏览: 19
好的,以下是使用 C# 和 Windows Forms 创建一个包含“座位号(i+1)”按钮组件的窗体的代码:
```csharp
using System;
using System.Windows.Forms;
namespace SeatSelection
{
public partial class SeatSelectionForm : Form
{
public SeatSelectionForm()
{
InitializeComponent();
CreateSeatButtons();
}
private void CreateSeatButtons()
{
const int buttonWidth = 50;
const int buttonHeight = 30;
const int buttonPadding = 10;
const int rows = 5;
const int columns = 10;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
int seatNumber = i * columns + j + 1;
Button button = new Button
{
Text = seatNumber.ToString(),
Width = buttonWidth,
Height = buttonHeight,
Top = i * (buttonHeight + buttonPadding) + buttonPadding,
Left = j * (buttonWidth + buttonPadding) + buttonPadding
};
button.Click += (sender, args) =>
{
MessageBox.Show($"座位{seatNumber}被选中");
};
Controls.Add(button);
}
}
}
}
}
```
这个程序会创建一个继承自 Windows Forms 的 Form 类的窗体,该窗体包含一个按钮组件。CreateSeatButtons() 方法会在窗体初始化时被调用,用于创建按钮组件。程序会创建一个 5 行 10 列的按钮矩阵,每个按钮的文本为对应的座位号。
当用户单击一个按钮时,程序会弹出一个消息框,显示“座位X被选中”的消息,其中X为座位编号。