如何在窗体末尾放置按钮,但是当AutoScroll = true时

时间:2014-04-20 13:42:41

标签: c# winforms

我有多个GroupBox,这就是我将AutoScroll设置为true的原因。我在Form_Load中创建所有控件。如何在所有GroupBox' es?

之后放置一个按钮

代码,我在其中创建GroupBox es:

 for (int i = 0; i < 10; i++)
            {
                GroupBox gb = new GroupBox();
                gb.Name = "GroupBox" + (i + 1);
                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.Name = "label" + (i + 1);
                q_text.Text = "Питання" + (i + 1);
                q_text.Font = new Font("Aria", 10, FontStyle.Bold);
                q_text.Location = new Point(10, 10);
                gb.Controls.Add(q_text);
                int iter = q_text.Location.Y + 30;
                if (i <= 5)
                {
                    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);
                    }
                }else
                    if (i > 5)
                    {
                        foreach (string key in questions[i].answers.Keys)
                        {
                            CheckBox rb = new CheckBox();
                            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 += 200;

2 个答案:

答案 0 :(得分:0)

由于您使用的是loc变量,因此可以执行以下操作:

btnMyButton.Locaction= new Point(40, loc);

无论如何,如果你想动态找到最后一个组合框的位置,试试这个:

double leftPos=0,topPos=0;
foreach(Control c in Forms.Controls)
{
    if(c.Name=="GroupBox")
    {
        if(c.Left>leftPos)
            leftPos=c.Left;
        if(c.Top>topPos)
            topPos=c.Top;
    }
}

答案 1 :(得分:0)

将所有可滚动组框放在一个设置为AutoScroll = true的面板中。此面板下方是另一个包含固定按钮的面板。此面板停靠在底部。