We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Scrollbar for canvas area

Rajmahesh asked
Medium Priority
Last Modified: 2009-07-29
I have set the canvas for whole Client Rectangle size.
When i resize the form, my form's clientrectangle size gets smaller than canvas area size and some of the graphic objects on screen gets hidden inside. Im creating a scrollbar horizontal/vertical (which ever is required) inorder to view the hidden area of the canvas. But when i scroll i couldn't make the form to move to show the hidden area.
I tried the below code,
// when horizontal scrollbar thumb is moved
        void hsb_ValueChanged(object sender, EventArgs e)
            isHsbValChanged = true;
            Invalidate(); // calling invalidate to raise paint event

        private void Form1_Paint(object sender, PaintEventArgs e)
            if (canvas.IsCanvasExists())
                canvas.g.FillRectangle(new SolidBrush(SystemColors.Control), e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height); // g is graphic object created in canvas form
                if (isHsbValChanged)
                    canvas.ScrollRender(canvas.g, 10, 0);
                    isHsbValChanged = false;

// Render function at canvas form
        public void Render(Graphics g)
            if (bmp != null)  //bmp is empty bitmap created at runtime
                // width & height are the canvas width & height
                g.DrawImage(bmp, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel);

// Scroll Render function at canvas form
        public void ScrollRender(Graphics g,int hVal, int vVal)
            if (bmp != null)
                // hVal is horizontal movement & vVal is vertical movement, values are taken just for example
                g.DrawImage(bmp, new Rectangle(0, 0, width, height), hVal, vVal, width + hVal, height + vVal, GraphicsUnit.Pixel);

But this code doesn't seems to work, i dont see the form area is moving.
How could i connect scrollbar to form's display(visible) area?
Any suggession would be of great help.
Thanks in advance.
Watch Question

Try to set Form.AutoScroll Property to true, this must solve the problem without any additional code.


Alex, I tried setting that, but that doesn't seem to be working.
Im not sure why it should not work. But the scrollbars doesn't appear when i resize the form.

Try to set AutoScrollMinSize to initial size of the canvas.


Got AutoScroll working after setting AutoScrollMinSize.
But the problem now is
I have grid lines in the form. When i resize i get the scrollbar. If i move the scrollbar rightside to some extent, during that time the form gets repainted and after that the visible client rectangle's top left corner becomes 0,0 and my grid lines are aligning relative to the visible client rectangle. I want only the view to get shifted not the grid even during repaint. The grid and any graphic objects in the form should be in the same position relative to the actual form's top left corner and not to form's visible area's top left corner. I guess this explains my problem. Please let me know if you require more detail.
I didn't understand exactly your requirements, but this problem should be solved using form AutoScrollPosition Property. If returns current scrollbars position, and you need to make correction with it. If some object is moved by scrollbar, and you want to keep it in the place, change it's position: X -= AutoScrollPosition.X, Y -= AutoScrollPosition.Y (pseudpcode).
If some object is not moved by scrollbar, and you want to move it, change it's position by the following way: X += AutoScrollPosition.X, Y += AutoScrollPosition.Y.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Here is my requirement:
I have a canvas drawn on the form. On top of it I have horizontal & vertical lines drawn at 50 pixels each. So i get the grid format. On top of it i have some graphic objects like Rectangle. When i drag the rectangle, it moves aligning to grid as like snaplines.
You got my problem right. But if the number of graphic objects (say rectangle) is less, then this would be feasible. But if the count is in hundreds (but in realcase it would not cross hundred) it would be a tough thing to set it for each object. I guess there should be an easy way to do this, since it is a common problem for all those who works on gdi+/graphic programming. Any suggessions. I will try the pseudocode you had given now.


Hi Alex,
Sorry I wasnt available for a week, thats y late reply.
Thanks for your time to help me.
I did the calculation for AutoScrollPosition in the wrapper class for each graphic object itself. So dint need any much work to fix it.
Thanks again.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.