【问题标题】:Childs grow to top-left instead of bottom-right孩子长到左上而不是右下
【发布时间】:2013-06-10 22:41:18
【问题描述】:

您如何处理autoscroll 仅在控件过度生长到底部或右侧而不是左上角时发生?

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

This guy had the same doubt,但答案建议转移到 WPF,这不是本项目的一个选项。

【问题讨论】:

  • 表单的向上和向左增长实际上是在将所有控件向右和向下移动并调整大小吗?控件的左上角始终带有坐标 (0,0),因此无论何时您将某些东西放在负坐标中,屏幕上都不会显示。
  • 嗨@NikolaDavidovic,面板位于表单的中间。你可以自己试试。面板和表格都没有长大。当面板的子控件的位置为负时,我只想让滚动条出现在面板上。你觉得有可能吗???
  • @Rafael 我认为你做不到。所有Visible region 始终包含 (0,0) 作为最左上角的可能点。所有 (-x,-y) 点都将不在视野范围内。我真的不明白你为什么想要这样的东西?因为坐标只是相对的。
  • 嗨@king-king 我允许用户动态创建控件,并将它们放在面板内。有时,控件会以这样的方式放置,即 X 或 Y 坐标均为负数。我以前从来没有注意到滚动只在右上角起作用的事实......直到现在我需要它。现在我阅读了你们所有人的 cmets,听起来它的可见区域从 0,0 开始是合理的。这是我昨天的课

标签: c# winforms .net-4.0 panel


【解决方案1】:

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

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

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

【讨论】:

  • 感谢@hans-passant,现在我一直在使用 AutoScrollMinSize,这听起来很合理。我将把控件移到 0,0 和右上角
【解决方案2】:

您可以通过在 (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?)

【讨论】:

  • 谢谢@jh00ker 这是我将采用的方法,只是想知道是否有一种自动的方法
猜你喜欢
  • 1970-01-01
  • 2019-04-15
  • 2016-05-02
  • 1970-01-01
  • 1970-01-01
  • 2011-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多