如何以编程方式滚动面板 [英] How to Programmatically Scroll a Panel

查看:133
本文介绍了如何以编程方式滚动面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我想以编程方式滚动板(垂直)向上或向下。

I am trying to programmatically scroll the panel (vertically) either up or down.

我曾尝试 AutoScrollPosition 属性设置为一个新的在面板上,但似乎并没有做到这一点。

I have tried setting the AutoScrollPosition property to a new Point on the panel but that doesn't seem to do it.

我有自动滚屏属性设置为true。

I have the AutoScroll property set to true.

我甚至尝试设置 VerticalScroll.Value 两倍建议这里,但这似乎并没有任何工作。

I even tried to set the VerticalScroll.Value twice as suggested here, but that doesn't seem to work either.

这是我目前做的:

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

在AutoScrollPosition X和Y值保持0和0。

The X and Y values on AutoScrollPosition remain 0 and 0.

在这个任何帮助或方向将不胜感激它。

Any help or direction on this would be greatly appreciated it.

谢谢,

马尔万

推荐答案

下面是一个解决方案。我想你可以滚动你的面板通过使用任意位置的Win32 然而,有一个简单的技巧来帮助你实现你的要求这里:

Here is a solution. I guess you can scroll your Panel by arbitrary position using Win32 however there is a simple trick to help you achieve your requirement here:

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



或者使用扩展方法方便:

Or use extension method for convenience:

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



更新



如果您想设置确切位置,修改略高于代码可以帮助:

UPDATE

If you want to set the exact position, modifying the code above a little can help:

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

您可能想使用另一种解决方案是使用面板.VerticalScroll.Value 。不过,我认为你需要更多的研究,使您预期的那样工作。因为我可以看到,一旦改变了,滚动条的位置和控制地位不会同步好。请注意, Panel.VerticalScroll.Value Panel.VerticalScroll.Minimum 面板之间。 VerticalScroll.Maximum

Another solution you may want to use is using Panel.VerticalScroll.Value. However I think you need more research to make it work as you expect. Because I can see once changing the Value, the scrollbar position and control position don't sync well. Notice that Panel.VerticalScroll.Value should be between Panel.VerticalScroll.Minimum and Panel.VerticalScroll.Maximum.

这篇关于如何以编程方式滚动面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆