面板奇怪的位置

时间:2014-05-31 22:49:47

标签: c# winforms

我有一个程序是某种测试。在这个测试中,我有一个函数AddQuestion,它添加了一个问题面板。为了逐个放置这些面板,我有一个变量loc,可以保存下一个面板的位置。有问题的前两个Panel添加正确,但下一个错误(远离底部)。它能是什么?

public void AddQuestion(int number, Question quest)
        {
            Panel p = new Panel();
            p.Name = "panel" + (number);
            p.Size = new Size(550, 400);
            p.Location = new Point(40, loc);
            p.BackColor = System.Drawing.Color.Bisque;
            p.AutoScroll = true;
            Panel pict_block = new Panel();
            pict_block.Size = new Size(480, 200);
            pict_block.Location = new Point(10, 10);
            PictureBox pict = new PictureBox();
            pict.Image = quest.image;
            pict.Size = new Size(240, 180);
            pict.SizeMode = PictureBoxSizeMode.StretchImage;
            pict.Location = new Point(130, 1);
            pict_block.Controls.Add(pict);
            p.Controls.Add(pict_block);

            Label number_text = new Label(); //номер питання
            number_text.Text = "Питання № " + number ;
            number_text.Font = new Font("Aria", 8, FontStyle.Bold);
            number_text.AutoSize = false;
            number_text.Location = new Point(400, 210);
            p.Controls.Add(number_text);

            Label q_text = new Label(); // текст питання
            q_text.Text = quest.question_text;
            q_text.Font = new Font("Aria", 9, FontStyle.Bold);
            q_text.AutoSize = false;
            q_text.Size = new Size(400, 50);
            q_text.Location = new Point(5, 220);
            p.Controls.Add(q_text);
            int iter = q_text.Location.Y + 60;
            if (CheckIfMuliple(number))
            {
                foreach (string key in quest.answers.Keys)
                {
                    CheckBox rb = new CheckBox();
                    rb.Text = key;
                    rb.AutoSize = true;
                    rb.Size = new Size(300, 25);
                    rb.Location = new Point(q_text.Location.X + 15, iter);
                    iter += 30;
                    p.Controls.Add(rb);
                }

            }
            else
                {
                    foreach (string key in quest.answers.Keys)
                    {
                        RadioButton rb = new RadioButton();
                        rb.Text = key;
                        rb.Size = new Size(300, 25);
                        rb.AutoSize = true;
                        rb.Location = new Point(q_text.Location.X + 10, iter);
                        iter += 30;
                        p.Controls.Add(rb);
                    }
                }
            questions_panel.Controls.Add(p);
            loc += 450;

        }

位置好: enter image description here

错误的位置: enter image description here

另外我注意到,当我添加一些面板时,然后在表单中间scrool并添加新问题,它不是位于底部,而是位于中心的某个位置。从下一个截图,6个问题,然后15个问题: enter image description here

3 个答案:

答案 0 :(得分:4)

    p.Location = new Point(40, loc);

滚动外面板​​时,这将无法正常工作。您必须通过该面板的滚动位置来抵消它。修正:

   p.Location = new Point(40  + questions_panel.AutoScrollPosition.X,
                          loc + questions_panel.AutoScrollPosition.Y);

   loc += 450;

当程序在具有以不同点/英寸设置运行的视频适配器的计算机上运行时,这将无法正常工作。如今非常普遍,Windows的现代版本使非常易于更改。面板将自动重新调整以匹配DPI设置。修正:

  loc += p.Height + 50;

答案 1 :(得分:2)

手动获取此功能非常困难。

我建议您将questions_panel更改为TableLayoutPanel。它会自动为您定位新的控件。

  

<强> TableLayoutPanel中

     

表示在网格中动态布局其内容的面板   由行和列组成。

答案 2 :(得分:1)

我能够复制你的问题,并且能够通过总是根据最后一个面板的位置偏移新面板的顶部来修复它。我改变了你的代码:

questions_panel.Controls.Add(p);
loc += 450;

为:

if (questions_panel.Controls.Count > 0)
{
    //Location of Top of last panel added then offset vertically by 450
    p.Location = new Point(p.Location.X, questions_panel.Controls[questions_panel.Controls.Count-1].Location.Y +450);
    questions_panel.Controls.Add(p);
}
else
    questions_panel.Controls.Add(p);