你能告诉我,请问如何在组合框中设置水平中心动态按钮?有没有像这样的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 < 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;
}
}
答案 0 :(得分:3)
您需要计算组框左边框的偏移量 如果有4个尺寸为40的按钮,则按钮会占用160像素的空间 如果组框的宽度为400像素,则
int leftOffset = (groupbox1.Width - (40 * numberOfButtons)) / 2; // (400 - 160) / 2
现在使用此偏移量
Buttons[r, c].Left = leftOffset + c * 40;
答案 1 :(得分:2)
这应涵盖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";
答案 2 :(得分:0)
尝试使用Fow Layout Panel添加按钮,例如简单的面板。