Sunday, March 20, 2011

How to scroll a panel manually?

I want to use the same functionality available when a Panel.AutoScroll is true, but with the scrollbars invisible.

To do so I need to know how can I scroll to left/right up/down using functions in my code.

From stackoverflow
  • You should be able to use the VerticalScroll and HorizontalScroll properties of the component:

    c.HorizontalScroll.Value += 100;
    c.VerticalScroll.Value = c.VerticalScroll.Maximum;
    
    Jonas : It works only when AutoScroll=true, but then I can't hide the HorizontalScroll/VerticalScroll. Setting the Scroll.Visible=false doesn't hide the scroll)
  • There's probably a property on the panel to do this, alternatively you can loop through all the panels children and adjust their positions.

    Eg. to move all controls 10 px:

    int xoffset = 10;
    
    foreach(Control c in panel1.Controls)
        c.Location.X += xoffset;
    

    The controls can be moved to negative positions to make them move out of the panel, similarly they can have location values bigger than the panels size to make them move out of the panel.

    fallenidol : don't think you can set the location to -ve values.
    sindre j : fallenidol : That is just plain wrong! Try it yourself, if you set the .Left property of a control to say -10 it will move left of the border of the container.
  • Well if you don't want to use the Autoscroll property, there's a way that I used a long time ago.

    • Put a panel inside the panel. Put the scrollbar control on the parent panel, and then use the scrollbar to change the Top property of the panel inside.

    It's simple and works beautifully.

0 comments:

Post a Comment