C# 如何将groupbox Windows窗体应用程序中的动态按钮居中
您能告诉我如何在groupbox中设置水平居中的动态按钮吗?是否有类似于textBox1.TextAlign=HorizontalAlignment.Center的内容C# 如何将groupbox Windows窗体应用程序中的动态按钮居中,c#,C#,您能告诉我如何在groupbox中设置水平居中的动态按钮吗?是否有类似于textBox1.TextAlign=HorizontalAlignment.Center的内容 private void Form1_Load(object sender, EventArgs e) { Button[,] Buttons; Buttons = new Button[4, 4]; int c,r; for (r = 0; r &
private void Form1_Load(object sender, EventArgs e)
{
Button[,] Buttons;
Buttons = new Button[4, 4];
int c,r;
for (r = 0; r < 4; r++) for (c = 0; c < 4; c++)
{
Buttons[r, c] = new Button();
Buttons[r, c].Parent = groupBox1;
Buttons[r, c].Top = 50 + r * 25;
Buttons[r, c].Left = 30 + c * 40;
Buttons[r, c].Width = 40;
}
}
private void Form1\u加载(对象发送方,事件参数e)
{
按钮[,]按钮;
按钮=新按钮[4,4];
int c,r;
for(r=0;r<4;r++)for(c=0;c<4;c++)
{
按钮[r,c]=新按钮();
按钮[r,c]。父项=groupBox1;
按钮[r,c]。顶部=50+r*25;
按钮[r,c]。左=30+c*40;
按钮[r,c]。宽度=40;
}
}
尝试使用像简单面板一样添加按钮。尝试像简单面板一样添加按钮。您需要计算组框左边框的偏移量。给定4个大小为40的按钮,按钮占用的空间为160像素。
如果groupbox的宽度为400像素,则
int leftOffset = (groupbox1.Width - (40 * numberOfButtons)) / 2; // (400 - 160) / 2
现在使用这个偏移量
Buttons[r, c].Left = leftOffset + c * 40;
您需要计算从groupbox左边框的偏移量。
给定4个大小为40的按钮,按钮占用的空间为160像素。
如果groupbox的宽度为400像素,则
int leftOffset = (groupbox1.Width - (40 * numberOfButtons)) / 2; // (400 - 160) / 2
现在使用这个偏移量
Buttons[r, c].Left = leftOffset + c * 40;
这应包括x轴和y轴对中:
Button[,] buttons = new Button[4, 4];
int c, r;
int xOffset = (groupBox1.Width - (40 * 4)) / 2;
int yOffset = (groupBox1.Height - 25 * 4) / 2;
for (r = 0; r < 4; r++) for (c = 0; c < 4; c++)
{
buttons[r, c] = new Button {Parent = groupBox1, Top = yOffset + r*25, Left = xOffset + c*40, Width = 40};
}
buttons[0, 0].Text = "1";
按钮[,]按钮=新按钮[4,4];
int c,r;
int xOffset=(groupBox1.Width-(40*4))/2;
int yOffset=(groupBox1.Height-25*4)/2;
for(r=0;r<4;r++)for(c=0;c<4;c++)
{
按钮[r,c]=新按钮{Parent=groupBox1,Top=yOffset+r*25,Left=xOffset+c*40,Width=40};
}
按钮[0,0]。Text=“1”;
这应包括x轴和y轴对中:
Button[,] buttons = new Button[4, 4];
int c, r;
int xOffset = (groupBox1.Width - (40 * 4)) / 2;
int yOffset = (groupBox1.Height - 25 * 4) / 2;
for (r = 0; r < 4; r++) for (c = 0; c < 4; c++)
{
buttons[r, c] = new Button {Parent = groupBox1, Top = yOffset + r*25, Left = xOffset + c*40, Width = 40};
}
buttons[0, 0].Text = "1";
按钮[,]按钮=新按钮[4,4];
int c,r;
int xOffset=(groupBox1.Width-(40*4))/2;
int yOffset=(groupBox1.Height-25*4)/2;
for(r=0;r<4;r++)for(c=0;c<4;c++)
{
按钮[r,c]=新按钮{Parent=groupBox1,Top=yOffset+r*25,Left=xOffset+c*40,Width=40};
}
按钮[0,0]。Text=“1”;
谢谢您的解释。谢谢您的解释。