Reputation: 333
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?
I explain: Place a panel in a winform, and place a button inside the panel. Make the location of the button something negative, like -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.
Upvotes: 0
Views: 383
Reputation: 343
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.
Instead of making the button's location (-20, -20), make it (0, 0). Next, iterate through all the other controls in the panel and move them each 20 pixels right and 20 pixels down. 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." Change the size of panel1 to (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.
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?)
Upvotes: 1
Reputation: 942207
That's just not how scrolling works. The logical upper-left corner of the panel is always (0,0). And is always visible in the upper left corner with the scrollbars at 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. 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.
Controls must always have a positive Location.X and Y value to be visible inside their container.
Upvotes: 2