TStringList image view[SOLVED] lazarus / 31-LCL ---------------------------------------------------------------------- Pe3s · 2024-06-17 09:16 Hello, how can I improve the code so that it loads the next and previous images after clicking the buttons? Regards ---------------------------------------------------------------------- paweld · 2024-06-17 10:14 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormCreate(Sender: TObject);begin   FImages := TStringList.Create;end; procedure TForm1.FormDestroy(Sender: TObject);begin  FImages.Free;end; procedure TForm1.SpeedButton1Click(Sender: TObject);begin  if OpenPictureDialog1.Execute then  begin    FImages.Clear;  //clear images list    try      FindAllFiles(FImages, ExtractFileDir(OpenPictureDialog1.FileName) + PathDelim, '*.jpg; *.jpeg', True);      Form1.Caption := IntToStr(FImages.IndexOf(OpenPictureDialog1.FileName)) +' / '+ IntToStr(FImages.Count);      //Image1.Picture.LoadFromFile(FImages[FI mages.IndexOf(OpenPictureDialog1.FileName)]);      FIIndex := FImages.IndexOf(OpenPictureDialog1.FileName);      if FIIndex < 0 then  //if the selected file is not found (is not JPG) then select the first one in the list        FIIndex := 0;       NextImage;    finally     end;  end;end; procedure TForm1.ShowImage(Sender: TObject); var  ImageIndex: integer;begin  if FImages.Count = 0 then //protect from an empty list    exit;  ImageIndex := FIIndex;  FIIndex := (FIIndex + 1) mod FImages.Count;  Image1.Picture.LoadFromFile(FImages[ImageIndex]);end; ---------------------------------------------------------------------- Pe3s · 2024-06-17 15:24 Thank you ---------------------------------------------------------------------- Pe3s · 2024-06-20 07:52 Hello, what do I need to correct to make the program count the number of images correctly, to count from 1 and not from zero? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  FileUtil, Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls,  ExtCtrls, Buttons, ExtDlgs, GR32_Image; type   { TForm1 }   TForm1 = class(TForm)    ImageList1: TImageList;    ImgView32_1: TImgView32;  OpenPictureDialog1: TOpenPictureDialog;    StatusBar1: TStatusBar;    ToolBar1: TToolBar;    ToolButton1: TToolButton;    ToolButton2: TToolButton;    ToolButton3: TToolButton;    ToolButton4: TToolButton;    procedure FormCreate(Sender: TObject);    procedure FormDestroy(Sender: TObject);    procedure ToolButton1Click(Sender: TObject);    procedure ToolButton3Click(Sender: TObject);    procedure ToolButton4Click(Sender: TObject);  private    FImages: TStringList;  FCurrentIndex: Integer;    procedure LoadImage(const APrev: Boolean);   public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin  FImages := TStringList.Create;end; procedure TForm1.FormDestroy(Sender: TObject);begin  FImages.Free;end; procedure TForm1.ToolButton1Click(Sender: TObject);begin  if OpenPictureDialog1.Execute then  begin      FImages.Clear;      FindAllFiles(FImages, ExtractFileDir(OpenPictureDialog1.FileName) + PathDelim, '*.jpg; *.jpeg', True);      FCurrentIndex := FImages.IndexOf(OpenPictureDialog1.FileName);      LoadImage(False);  end;end; procedure TForm1.ToolButton3Click(Sender: TObject);begin  LoadImage(True);end; procedure TForm1.ToolButton4Click(Sender: TObject);begin  LoadImage(False);end; procedure TForm1.LoadImage(const APrev: Boolean);var  ImageIndex: Integer;begin  ImageIndex := FCurrentIndex;   if APrev then  begin     if FCurrentIndex > 0 then  Dec(FCurrentIndex)  end else  begin    if FCurrentIndex < FImages.Count -1 then    Inc(FCurrentIndex);  end;   ImgView32_1.Bitmap.LoadFromFile(FImages[ImageIndex]);   Form1.Caption := ExtractFileName(FImages[ImageIndex]);  StatusBar1.Panels[0].Text := IntToStr(ImgView32_1.Bitmap.Width) +'x'+ IntToStr(ImgView32_1.Bitmap.Height);  StatusBar1.Panels[1].Text := IntToStr(FCurrentIndex) +' / '+ IntToStr(FImages.Count);end; end. ---------------------------------------------------------------------- alpine · 2024-06-20 08:15 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i StatusBar1.Panels[1].Text := IntToStr(FCurrentIndex + 1) +' / '+ IntToStr(FImages.Count);? ---------------------------------------------------------------------- Pe3s · 2024-06-20 08:33 Now he counts from 2 ---------------------------------------------------------------------- alpine · 2024-06-20 08:48 Quote from: Pe3s on June 20, 2024, 10:33:22 amNow he counts from 2 It does not. ---------------------------------------------------------------------- paweld · 2024-06-20 12:11 @alpine provided the correct code, but it is misplaced in your procedure code. Since you immediately change the current index when you press the button, you need to place this line of code at the beginning of the procedure. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.LoadImage(const APrev: Boolean);var  ImageIndex: Integer;begin  ImageIndex := FCurrentIndex;  StatusBar1.Panels[1].Text := IntToStr(FCurrentIndex + 1) +' / '+ IntToStr(FImages.Count);    if APrev then  begin     if FCurrentIndex > 0 then    Dec(FCurrentIndex)  end else  begin    if FCurrentIndex < FImages.Count -1 then    Inc(FCurrentIndex);  end;   ImgView32_1.Bitmap.LoadFromFile(FImages[ImageIndex]);   Form1.Caption := ExtractFileName(FImages[ImageIndex]);  StatusBar1.Panels[0].Text := IntToStr(ImgView32_1.Bitmap.Width) +'x'+ IntToStr(ImgView32_1.Bitmap.Height);end; ---------------------------------------------------------------------- alpine · 2024-06-20 13:05 And I bet there will be a little surprise when the direction is changed  Edit: To be a bit more clear - modifying FCurrentIndex after the loading of the image file will have the (unexpected) effect of showing the result of the previous movement. ---------------------------------------------------------------------- Pe3s · 2024-06-20 19:45 exactly the other way around doesn't work ---------------------------------------------------------------------- alpine · 2024-06-21 08:00 Quote from: Pe3s on June 20, 2024, 09:45:28 pmexactly the other way around doesn't work I don't know what do you mean by "the other way around", perhaps it is changing FCurrentIndex before the loading, but actually that's the way it should work if you don't want your current picture to lag one step behind. One simple way to do it: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.LoadImage(const AInc: Integer);var  ImageIndex: Integer;begin  ImageIndex := FCurrentIndex + AInc;  if (ImageIndex < 0) or (ImageIndex >= FImages.Count) then     Exit;  ImgView32_1.Bitmap.LoadFromFile(FImages[ImageIndex]);  Form1.Caption := ExtractFileName(FImages[ImageIndex]);  StatusBar1.Panels[0].Text := IntToStr(ImgView32_1.Bitmap.Width) +'x'+ IntToStr(ImgView32_1.Bitmap.Height);  StatusBar1.Panels[1].Text := IntToStr(ImageIndex + 1) +' / '+ IntToStr(FImages.Count);  FCurrentIndex := ImageIndex;end; Then call LoadImage(0) on initial loading, call LoadImage(1) when 'Next' clicked and call LoadImage(-1) when 'Prev' clicked. Another way would be not to modify FCurrentIndex into the LoadImage(), i.e. just load and show texts, and to do incrementing/decrementing into Next/Prev handlers. It's pretty trivial and I don't know why you expect someone to do it for you. ---------------------------------------------------------------------- Pe3s · 2024-06-24 10:04 Thank you