简体   繁体   English

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

[英]Childs grow to top-left instead of bottom-right

How do you deal with the autoscroll only happening when the control is overgrowth to the bottom or right, but not to the top-left? 仅当控件在底部或右侧过度增长而在左上方过度autoscroll时,如何处理autoscroll

I explain: Place a panel in a winform, and place a button inside the panel. 我将解释:将面板放置在winform中,然后在面板内部放置一个按钮。 Make the location of the button something negative, like -20, -20. 使按钮的位置为负数,例如-20,-20。 Scrollbars do not appear 滚动条不出现

This guy had the same doubt , but answers suggested move to WPF, which is not an option in this project. 这个人也有同样的疑问 ,但是回答建议改用WPF,这不是该项目的选择。

That's just not how scrolling works. 那不是滚动的工作原理。 The logical upper-left corner of the panel is always (0,0). 面板的逻辑左上角始终为(0,0)。 And is always visible in the upper left corner with the scrollbars at 0. 并且始终在左上角显示,滚动条为0。

You get the exact same outcome you are looking for by simply making the panel's AutoScrollMinSize property bigger by 20x20 and moving all of the controls over by +20,+20. 只需将面板的AutoScrollMinSize属性增大20x20,然后将所有控件移动+ 20,+ 20,即可得到与您期望的结果完全相同的结果。 Which now of course makes that button visible. 当然,现在哪个按钮可见。 And adjusted the scrollbars as well, they have a bigger range. 并调整了滚动条,它们的范围更大。 If you use AutoScroll then just moving the controls is sufficient. 如果使用AutoScroll,则仅移动控件就足够了。

Controls must always have a positive Location.X and Y value to be visible inside their container. 控件必须始终具有正的Location.X和Y值,以使其在容器内可见。

You can simulate adding the button off the panel in the upper-left region by adding at (0, 0) and then moving the panel's displayed area down and to the right. 您可以通过在(0,0)处进行添加,然后将面板的显示区域上下左右移动,来模拟在面板的左上方区域中添加按钮。

Instead of making the button's location (-20, -20), make it (0, 0). 而不是将按钮的位置(-20,-20)设置为(0,0)。 Next, iterate through all the other controls in the panel and move them each 20 pixels right and 20 pixels down. 接下来,遍历面板中的所有其他控件,并分别向右移动20个像素和向下移动20个像素。 Finally, scroll the panel down and to the right. 最后,向下和向右滚动面板。

    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);
    } 

To run the sample code, add a "panel1" to a form called "Form1." 要运行示例代码,请将“ panel1”添加到名为“ Form1”的表单中。 Change the size of panel1 to (111, 115). 将panel1的大小更改为(111,115)。 Add three buttons to panel1 called "button1," button2" and "button3." Add two buttons to the form called "btnMoveToUpperLeft" and "btnResetPosition." Paste the sample code into Form1.cs. 将三个按钮添加到panel1中,分别称为“ button1”,“ button2”和“ button3”。将两个按钮添加到名为“ btnMoveToUpperLeft”和“ btnResetPosition”的表单中。将示例代码粘贴到Form1.cs中。

Note that the code for moving the scrollbars looks funny because of a bug where just setting the scrollbar equal to the value causes the scrollbar not to update. 请注意,用于移动滚动条的代码看起来很有趣,因为存在一个错误,即仅将滚动条设置为等于该值会导致滚动条不更新。 ( How to scroll a panel manually? ) 如何手动滚动面板?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM