孩子长到左上角而不是右下角

时间:2013-06-10 22:41:19

标签: c# winforms .net-4.0 panel

如果控件过度生长到底部或右边而不是左上角,你如何处理autoscroll

我解释:将面板放在winform中,然后在面板内放置一个按钮。使按钮的位置为负,例如-20,-20。滚动条不会出现

This guy had the same doubt,但建议回答WPF,这不是此项目的选项。

2 个答案:

答案 0 :(得分:2)

这不是滚动的工作方式。面板的逻辑左上角始终为(0,0)。并且在左上角始终可见,滚动条位于0。

通过简单地将面板的AutoScrollMinSize属性设置为20x20并将所有控件移动+ 20,+ 20,您可以获得与完全相同的结果。现在当然使该按钮可见。并调整了滚动条,它们的范围更大。如果您使用AutoScroll,那么只需移动控件即可。

控件必须始终在其容器内显示正的Location.X和Y值。

答案 1 :(得分:1)

您可以通过添加at(0,0)然后向下和向右移动面板的显示区域来模拟在左上角区域的面板上添加按钮。

不要使按钮的位置(-20,-20),而是使它(0,0)。 接下来,遍历面板中的所有其他控件,并将它们向右移动20个像素,向下移动20个像素。 最后,将面板向下滚动到右侧。

    private void Form1_Load(object sender, EventArgs e)
    {
        btnResetPosition_Click(sender, e);
    }

    private void btnMoveToUpperLeft_Click(object sender, EventArgs e)
    {
        //Set Panel View to upper-left before moving around buttons
        panel1.VerticalScroll.Value = panel1.VerticalScroll.Value = panel1.VerticalScroll.Minimum;
        panel1.HorizontalScroll.Value = panel1.HorizontalScroll.Value = panel1.HorizontalScroll.Minimum;

        //Move button1 to "upper-left"
        button1.Location = new Point(0, 0);

        //Adjust "static" controls right and down to simulate moving button1
        button2.Location = new Point(button2.Location.X + 200, button2.Location.Y + 200);
        button3.Location = new Point(button3.Location.X + 200, button3.Location.Y + 200);

        //Scroll to show "static" controls in panel
        panel1.VerticalScroll.Value = panel1.VerticalScroll.Value = panel1.VerticalScroll.Maximum;
        panel1.HorizontalScroll.Value = panel1.HorizontalScroll.Value = panel1.HorizontalScroll.Maximum;
    }

    private void btnResetPosition_Click(object sender, EventArgs e)
    {
        //Set Panel View to upper-left before moving around buttons
        panel1.VerticalScroll.Value = panel1.VerticalScroll.Value = panel1.VerticalScroll.Minimum;
        panel1.HorizontalScroll.Value = panel1.HorizontalScroll.Value = panel1.HorizontalScroll.Minimum;

        //Line up all three buttons
        button1.Location = new Point(19, 17);  // 19 and 17 are used so that when panel scrollbars appear, "static" controls appear to stay in the same place
        button2.Location = button1.Location;
        button2.Location = new Point(button1.Location.X, button1.Location.Y + 29);
        button3.Location = button2.Location;
        button3.Location = new Point(button2.Location.X, button2.Location.Y + 29);
    } 

要运行示例代码,请将“panel1”添加到名为“Form1”的表单中。将panel1的大小更改为(111,115)。向panel1添加三个按钮,名为“button1”,“button2”和“button3。”将两个按钮添加到名为“btnMoveToUpperLeft”和“btnResetPosition”的表单中。将示例代码粘贴到Form1.cs中。

请注意,移动滚动条的代码看起来很有趣,因为只有将滚动条设置为等值才会导致滚动条不更新。 (How to scroll a panel manually?

相关问题