为什么它只填充第一个组框?

时间:2014-04-20 12:29:47

标签: c# windows

我有一个测试,在Form1_Load我用问题和答案填写这个测试。但它只显示了第一个问题。怎么了?

int loc = 20;
for (int i = 0; i < 5; i++)
{
    GroupBox gb = new GroupBox();
    gb.Size = new Size(500, 200);
    gb.Location = new Point(40, loc);
    gb.BackColor = System.Drawing.Color.Aquamarine;

    Label q_text = new Label(); // текст питання
    q_text.Text = "Питання" + (i + 1);
    q_text.Font = new Font("Aria", 10, FontStyle.Bold);
    q_text.Location = new Point(gb.Location.X, gb.Location.Y);
    gb.Controls.Add(q_text);
    int iter = q_text.Location.Y + 30;

    foreach (string key in questions[i].answers.Keys)
    {
        RadioButton rb = new RadioButton();
        rb.Text = key;
        rb.Size = new Size(120, 25);
        rb.Location = new Point(q_text.Location.X + 10, iter);
        iter += 30;
        gb.Controls.Add(rb);
    }

    this.Controls.Add(gb);
    loc += 300;
}

1 个答案:

答案 0 :(得分:0)

检查代码后,q_text

会产生一些问题
int loc = 20;
for (int i = 0; i < 5; i++)
{
    GroupBox gb = new GroupBox();
    gb.Size = new Size(500, 200);
    gb.Location = new Point(40, loc);
    gb.BackColor = System.Drawing.Color.Aquamarine;

    Label q_text = new Label(); // текст питання
    q_text.Text = "Питання" + (i + 1);
    q_text.Font = new Font("Aria", 10, FontStyle.Bold);
    q_text.Location = new Point(0, 0);
    gb.Controls.Add(q_text);
    int iter = q_text.Location.Y + 30;

    foreach (string key in questions[i].answers.Keys)
    {
        RadioButton rb = new RadioButton();
        rb.Text = key;
        rb.Size = new Size(120, 25);
        rb.Location = new Point(q_text.Location.X + 10, iter);
        iter += 30;
        gb.Controls.Add(rb);
    }

    this.Controls.Add(gb);
    loc += 300;
}

此处的唯一更改是q_text设置为0,0

我要做的另一项更改是根据q_text的身高调整高度:

int iter = q_text.Location.Y + q_text_Size.Height + 5;

以防万一标签很长且包裹文字。