1
votes

I have a Delphi form with a TScrollBox and some TImage components, and the form's scrollbox is not resetting when it's emptied... it appears to be growing every time a new image is thrown in the box.

I would like to reset the scroll range/size to the scrollbox size after I delete the image, before the next one is loaded. Is there a way to do that?

I've tried setting scrollbars invisible, and turning them back on after the next file loads and that doesn't seem to work. Any help greatly appreciated.

Root Cause: So it appears that the image moves its top-left corner to the center of where the image was located in the TScrollBox when the bitmap is freed.

2
Please provide a Minimal, Complete, and Verifiable example. What does your code and DFM look like? Do you have the TForm.AutoSize set to true or false? The TScrollBox.AutoSize? TImage.AutoSize? It sounds like you are not actually resizing the ScrollBox correctly, so its content area is not shrinking like you expect it to.Remy Lebeau
ScrollBox1.AutoScroll = true; ScrollBox1.AutoSize := false; image1.AutoSize := True; I may have solved the issue.. will verify this tomorrow.Joseph Poirier
@JosephPoirier, please rollback your latest edit and put in a separate answer.LU RD
Done. Answer posted belowJoseph Poirier

2 Answers

1
votes

Root Cause : So it appears that the image moves its top-left corner to the center of where the image was located in the TScrollBox when the bitmap is freed.

Resolution : Move image to top after turning off scrollbars and freeing image, but before loading new image into image object.

Code Sample..

try
  // Reset existing images
  if assigned(Image1.Picture.Bitmap) then
    Image1.Picture.Bitmap.FreeImage; // using .Free eventually caused memory issues
    // .Free should only be in Finally code section for process objects
    // or on Destroy event for program objects

  Image1.Picture.Graphic := TBitmap.Create;
  Image1.Picture.Bitmap := TBitmap.Create;

  // reset Bitmap
  if assigned(bitmap123) then
    bitmap123.FreeImage;

  bitmap123 := TBitmap.Create;

finally
  ScrollBox1.HorzScrollBar.Visible := false;
  ScrollBox1.VertScrollBar.Visible := false;
  Image1.Top := 0; Image1.Left := 0;
  Image1.Refresh;
  Application.ProcessMessages;

  ScrollBox1.HorzScrollBar.Visible := true;
  ScrollBox1.VertScrollBar.Visible := true;
  ScrollBox1.Refresh;

end;
// now images can be loaded 
// and they will appear in the top-left corner of the scrollbox every time.
0
votes

I am not sure how your things look, but here is what I suggest you have a look at:

  • Make the ScrollBox1.AutoSize := TRUE
  • Check the Range property of the Horizontal/Vertical Scrollbar.
  • Make sure that there isn't actually something on the ScrollBox causing this.

Alternatively you could just recreate the whole Scrollbar also, but I don't think this is what you want to do.