孩子长到左上角而不是右下角
本文关键字:右下角 左上角 孩子 | 更新日期: 2023-09-27 18:17:59
当控件过度生长到底部或右侧而不是左上角时,您如何处理autoscroll
?
我解释:在窗体中放置一个面板,并在面板中放置一个按钮。将按钮的位置设置为负值,比如-20,-20。滚动条不出现
这个家伙也有同样的疑问,但答案建议转移到WPF,这在这个项目中不是一个选项。
这不是滚动的工作原理。面板的逻辑左上角总是(0,0)。并且在左上角滚动条为0时始终可见。
你得到确切的相同的结果,你正在寻找通过简单地使面板的AutoScrollMinSize属性增大20x20和移动所有的控件+20,+20。这样按钮就可见了。也调整了滚动条,它们的范围更大了。如果您使用自动滚动,那么只需移动控件就足够了。
控件必须总是有一个正的Location。
您可以通过添加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);
}
要运行示例代码,向名为"Form1"的表单添加"panel1"。改变panel1的大小为(111,115)。向panel1添加三个按钮,分别为"button1"、"button2"answers"button3"。向表单添加两个按钮,分别名为"btnMoveToUpperLeft"answers"btnResetPosition"。将示例代码粘贴到Form1.cs.
注意,移动滚动条的代码看起来很奇怪,因为有一个错误,即仅仅将滚动条设置为等于该值就会导致滚动条不更新。(如何手动滚动面板