0

autoscroll当控件过度生长到底部或右侧而不是左上角时,您如何处理唯一发生的情况?

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

这个人也有同样的疑问,但答案建议转移到 WPF,这不是这个项目中的一个选项。

4

2 回答 2

2

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

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

控件必须始终具有正的 Location.X 和 Y 值才能在其容器内可见。

于 2013-06-11T01:10:54.060 回答
1

您可以通过在 (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 中。

请注意,移动滚动条的代码看起来很有趣,因为有一个错误,即仅将滚动条设置为等于该值会导致滚动条不更新。(如何手动滚动面板?

于 2013-06-11T01:46:28.300 回答