How to Programmatically Scroll a Panel

后端 未结 7 2055
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 06:57

I have a System.Windows.Forms.Panel with some content.

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

I have t

7条回答
  •  一向
    一向 (楼主)
    2020-11-29 07:10

    Create an control that sits slightly outside the visible area (so -1 at the top and clientsize+1 ) and then call 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();
    }
    

    with yourpanel.SetAutoScrollMargin(1, 1); you can set very fine scrolling steps and then take a timer to call the srolling when buttons are down

提交回复
热议问题