Problem with Image inside TScrollBox and scrolling

dabestprogrammerindauniverse used Ask the Experts™
Hi everyone,

I have a TImage inside a TScrollBox. The image is bigger in dimension than the TScrollBox so scrollbars appear on the TScrollBox. I redraw/refresh the TImage everytime it is clicked via onmousedown event. My problem is when the area I click is in the non-visible part of the TImage, which means I have to scroll down or to the right. After redrawing/refreshing the TImage the scrollbars of the scrollbox return to 0 position. I use the following code to scroll back to the non-visible part where the mouse was clicked:

i := GetScrollPos(SBx_DayView.Handle, SB_HORZ);
SetScrollPos(SBx_DayView.Handle, SB_HORZ, i, true);
j := GetScrollPos(SBx_DayView.Handle, SB_VERT);
SetScrollPos(SBx_DayView.Handle, SB_VERT, j, true);

The scrollbox scrolls but view is not scrolled to the (non-visible) part of the image where the mouse was clicked.

I've also tried using:

xpos := SBx_DayView.HorzScrollBar.ScrollPos;
ypos := SBx_DayView.VertScrollBar.ScrollPos;
//refresh image code here
SBx_DayView.ScrollBy(xpos, ypos);


xpos := SBx_DayView.HorzScrollBar.ScrollPos;
ypos := SBx_DayView.VertScrollBar.ScrollPos;
//refresh image code here
SBx_DayView.HorzScrollBar.Position := xpos;
 SBx_DayView.VertScrollBar.Position := ypos;

but none of those work.

Can anyone suggest an workaround?

Thanks in advance :)
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Russell LibbySoftware Engineer, Advisory
Top Expert 2005

Can you the show the code used for "refreshing" the TImage? If the width / height of the image does not change, it would seem like it would be better to get the code to work so that the scroll pos does not reset.

Senior Developer
I do not get a problem calling .Refresh or .Repaint
Is it possible that you are resizing the TImage/recreating the TImage, and that is why the scrollbars are reverting to 0,0 ?
If you are reloading the image from disk, or doing a "image1.picture.assign(mypic)" you are likely to get this problem.
If you are trying to draw on the timage, try just paiting to the .Picture.Bitmap.Canvas property
Hi guys,

I have discovered the problem and indeed as you guys have said it has to do with resizing the TImage. I've commented the image resize code and it works perfectly.

Sorry but I have found the solution a few minutes after i've posted my question, but am willing to split the points up for the effort.


Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial