23

我有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);

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

对此的任何帮助或指导将不胜感激。

提前致谢,

马旺

4

8 回答 8

25

这是一个解决方案。我想你可以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);

或者为了方便使用扩展方法:

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

更新

如果你想设置准确的位置,稍微修改一下上面的代码会有所帮助:

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

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

于 2013-07-19T19:25:00.313 回答
7

这令人惊讶的工作!注意代码中的减号。设置滚动位置有奇怪的行为。如果将位置设置为精确值 (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;
}

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

于 2018-09-12T19:04:30.967 回答
4

如果您有一个派生自 的类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);

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

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

保留AutoScroll其默认值“true”。

于 2016-11-14T17:25:54.967 回答
4

试试这个:- panel.ScrollControlIntoView(childcontrol);

这应该有效。childcontrol 是您要在显示区域中显示的特定控件。

于 2017-03-23T17:54:29.537 回答
1

使用@King King Answered Code,如果要隐藏水平和垂直滚动条,只需在构造函数或初始化中应用以下代码。

        yourPanel.AutoScroll = false;
        yourPanel.HorizontalScroll.Maximum = 0;
        yourPanel.HorizontalScroll.Visible = false;
        yourPanel.VerticalScroll.Maximum = 0;
        yourPanel.VerticalScroll.Visible = false;
        yourPanel.AutoScroll = true;
于 2016-06-27T09:40:51.157 回答
1

设置HorizontalScroll属性的值,然后使用该方法ScrollControlIntoView对我有用:

lpanel.HorizontalScroll.Value = 100;
lpanel.ScrollControlIntoView(lpanel);
于 2021-08-26T09:46:46.953 回答
1

我遇到了一个问题,我无法让我的面板滚动回顶部。我尝试了很多方法来尝试让面板在填充了许多控件后滚动回顶部。

不管我做什么,它总是把 VScroll 栏放在底部。

经过详尽的测试后,我发现这是因为我的控件将TabStop属性设置为 true(用户控件上的默认值)导致了问题。

将 TabStop 设置为false修复了它。

于 2017-04-25T14:55:29.393 回答
0

创建一个稍微位于可见区域之外的控件(顶部为 -1,clientsize+1),然后调用 ScrollControlIntoView:

public static class PanelExtension {
public static void ScrollDown(this Panel p)
{

    using (Control c = new Control() { Parent = p, Height = 1, Top = p.ClientSize.Height + 1 })
    {
        p.ScrollControlIntoView(c);                
    }
}
public static void ScrollUp(this Panel p )
{

    using (Control c = new Control() { Parent = p, Height = 1, Top = -1})
    {
        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.

        private void buttonUp_Click(object sender, EventArgs e)
{

   yourPanel.ScrollUp();
}
private void buttonDown_Click(object sender, EventArgs e)
{

   yourPanel.ScrollDown();
}

与 yourpanel.SetAutoScrollMargin(1, 1); 您可以设置非常精细的滚动步骤,然后在按钮按下时使用计时器来调用滚动

于 2019-09-06T08:29:07.750 回答