NET 4.5 C#创建一个Windows窗体.我想动态创建和添加按钮并为它们分配点击事件,但希望它们像图像一样以特定方式动态放置.
我的问题是如何以上述方式动态放置按钮,即4x4格式(连续4个按钮,4列但无限行).是否有可能以获胜形式这样做?
目前我正在尝试下面提到的代码,但对于如何放置如上所示的按钮没有明确的想法.
public Form1()
{
InitializeComponent();
for (int i = 0; i < 5; i++)
{
Button button = new Button();
button.Location = new Point(160, 30 * i + 10);
button.Click += new EventHandler(ButtonClickCommonEvent);
button.Tag = i;
this.Controls.Add(button);
}
}
void ButtonClickCommonEvent(object sender, EventArgs e)
{
Button button = sender as Button;
if (button != null)
{
switch ((int)button.Tag)
{
case 0:
// First Button Clicked
break;
case 1:
// Second Button Clicked
break;
// ...
} …Run Code Online (Sandbox Code Playgroud)