如何以编程方式滚动面板

Mar*_*وان 15 c# scroll panel winforms

我有System.Windows.Forms.Panel一些内容.

我试图以编程方式向上或向下滚动面板(垂直).

我已经尝试将该AutoScrollPosition属性设置为Point面板上的新属性,但似乎没有这样做.

我将AutoScroll属性设置为true.

我甚至试图在这里设置VerticalScroll.Value两次,但这似乎也不起作用.

这就是我目前正在做的事情:

//I have tried passing both positive and negative values.
panel.AutoScrollPosition = new Point(5, 10);
Run Code Online (Sandbox Code Playgroud)

AutoScrollPosition上的X和Y值保持为0和0.

任何帮助或方向将非常感谢它.

提前致谢,

马尔万

Kin*_*ing 15

这是一个解决方案.我想你可以Panel使用任意位置滚动你,Win32但有一个简单的技巧可以帮助你达到你的要求:

public void ScrollToBottom(Panel p){
  using (Control c = new Control() { Parent = p, Dock = DockStyle.Bottom })
     {
        p.ScrollControlIntoView(c);
        c.Parent = null;
     }
}
//use the code
ScrollToBottom(yourPanel);
Run Code Online (Sandbox Code Playgroud)

或者使用扩展方法以方便:

public static class PanelExtension {
   public static void ScrollToBottom(this Panel p){
      using (Control c = new Control() { Parent = p, Dock = DockStyle.Bottom })
      {
         p.ScrollControlIntoView(c);
         c.Parent = null;
      }
   }
}
//Use the code
yourPanel.ScrollToBottom();
Run Code Online (Sandbox Code Playgroud)

UPDATE

如果你想设置确切的位置,修改上面的代码可以帮助:

//This can help you control the scrollbar with scrolling up and down.
//The position is a little special.
//Position for scrolling up should be negative.
//Position for scrolling down should be positive
public static class PanelExtension {
    public static void ScrollDown(this Panel p, int pos)
    {
        //pos passed in should be positive
        using (Control c = new Control() { Parent = p, Height = 1, Top = p.ClientSize.Height + pos })
        {
            p.ScrollControlIntoView(c);                
        }
    }
    public static void ScrollUp(this Panel p, int pos)
    {
        //pos passed in should be negative
        using (Control c = new Control() { Parent = p, Height = 1, Top = pos})
        {
            p.ScrollControlIntoView(c);                
        }
    }
}
//use the code, suppose you have 2 buttons, up and down to control the scrollbar instead of clicking directly on the scrollbar arrows.
int i = 0;
private void buttonUp_Click(object sender, EventArgs e)
{
   if (i >= 0) i = -1;
   yourPanel.ScrollUp(i--);
}
private void buttonDown_Click(object sender, EventArgs e)
{
   if (i < 0) i = 0;
   yourPanel.ScrollDown(i++);
}
Run Code Online (Sandbox Code Playgroud)

您可能想要使用的另一种解决方案是使用Panel.VerticalScroll.Value.但是我认为你需要更多的研究才能使它按预期工作.因为我可以看到一次更改Value,滚动条位置和控制位置不能很好地同步.请注意,Panel.VerticalScroll.Value应该在Panel.VerticalScroll.Minimum和之间Panel.VerticalScroll.Maximum.


Mil*_*vec 6

这令人惊讶的工作!注意代码中的减号。设置滚动位置有奇怪的行为。如果您将位置设置为精确值 (50),则下次读取时它会变为负值 (-50)。所以你必须在设置新的滚动值之前反转它。

向下滚动:

private void ButtonScrollDown_OnClick(object sender, EventArgs e)
{
    Point current = yourScrollPanel.AutoScrollPosition;
    Point scrolled = new Point(current.X, -current.Y + 10);
    yourScrollPanel.AutoScrollPosition = scrolled;
}
Run Code Online (Sandbox Code Playgroud)

类似地向上滚动, (-current.Y - 10)

  • 干这个?`panel1.AutoScrollPosition = new Point(-panel1.AutoScrollPosition.X + 200);` (2认同)

Ron*_*Ron 5

如果您有一个派生自 的类Panel,则调用这两个受保护的方法来滚动面板:

// The bottom is off screen; scroll down. These coordinates must be negative or zero.
SetDisplayRectLocation(0, AutoScrollPosition.Y - item.BoundingRect.Bottom + ClientRectangle.Bottom);
AdjustFormScrollbars(true);
Run Code Online (Sandbox Code Playgroud)

在我的示例中,item.BoundingRect.Bottom是缩略图底部的 Y 坐标,我需要向下滚动面板以便整个缩略图可见。

@King King 创建临时控件以便可以完成滚动的解决方案对我来说似乎“沉重”。@Hans Passant 的设置建议对我不起作用AutoScrollMinSizeAutoScrollPosition

保留AutoScroll其默认值“true”。