[SOLVED] TPanel slider position lazarus / 31-LCL ---------------------------------------------------------------------- Pe3s · 2023-11-28 10:09 Hello forum members, what do I need to correct in the code to make the adjustment range -100 - 0 - 100 work? 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.Panel2MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);begin  if not(ssLeft in Shift) then Exit;  Pos := X + Panel2.Left;  Max := Panel1.ClientWidth - Panel2.Width;  if Pos < 0 then Pos := 0;  if Pos > Max then Pos := Max;  Panel2.Left := Pos;   BGProgress.Value := Round(100 * Pos / Max);  Form1.Caption := IntToStr(BGProgress.Value);end; procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton;  Shift: TShiftState; X, Y: Integer);begin  if not (ssLeft in Shift) then Exit;  Pos:= X;  Max := Panel1.ClientWidth - Panel2.Width;  if Pos < 0 then Pos := 0;  if Pos > Max then Pos := Max;  Panel2.Left:= Pos;   BGProgress.Value := Round(100 * Pos / Max);  Form1.Caption := IntToStr(BGProgress.Value);end; ---------------------------------------------------------------------- Handoko · 2023-11-28 12:59 Done. This is the correct formula: BGProgress.Value := Round(200 * Pos / Max) - 100; ---------------------------------------------------------------------- Pe3s · 2023-11-28 15:20 @Handoko, Thank you for your help ---------------------------------------------------------------------- Thaddy · 2023-11-29 08:21 I believe the correct formula to be BGProgress.Value := (200 * Pos  div Max) - 100;, Handoko. That processes completely in the integer domain. ---------------------------------------------------------------------- Handoko · 2023-11-30 03:07 Yes, you're right. I forgot that div is more efficient than / for result in integer. ---------------------------------------------------------------------- Pe3s · 2023-12-02 18:19 I have one more question why the code works with Panel components but not with Image ? 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.Panel2MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);var  NewX, Max: Integer;begin  if not(ssLeft in Shift) then Exit;  NewX := X + Panel2.Left -20;  Max := Panel1.ClientWidth - Panel2.Width;  if NewX < 0 then NewX := 0;  if NewX > Max then NewX := Max;  Panel2.Left := NewX;   Label1.Caption := GetPosition.ToString;end; ---------------------------------------------------------------------- Handoko · 2023-12-02 18:50 I modified your code by renaming Panel2 to Image1, it works. If you can't make it works, please provide a demo so I can download and inspect the problem. ---------------------------------------------------------------------- Pe3s · 2023-12-02 20:19 TImage counts Positions differently ---------------------------------------------------------------------- Handoko · 2023-12-03 03:08 Done! 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  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Image1: TImage;    Image2: TImage;    Label1: TLabel;    procedure Image2MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer      );  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Image2MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);const  ResultMin = 0;  ResultMax = 100;var  AllowedMin,  AllowedMax,  Result:      Integer;begin   if not(ssLeft in Shift) then Exit;   AllowedMin := Image1.Left;  AllowedMax := Image1.Left + Image1.Width - Image2.Width;   Image2.Left := Image2.Left + X - (Image2.Width div 2);  if Image2.Left < AllowedMin then Image2.Left := AllowedMin;  if Image2.Left > AllowedMax then Image2.Left := AllowedMax;   Result := ((ResultMax-ResultMin) *            (Image2.Left-AllowedMin) div            (AllowedMax-AllowedMin)) + ResultMin;  Label1.Caption := Result.ToString; end; end. First of all, lets talk about moving the handle. See line #46. That may seem complicated. But actually the basic formula is very simple: Image2.Left := Image2.Left + X;Now, see the img2 below. Because the handle has width, to make the the pointer to detect the center of the handle, it needs to do: - (Image2.Width div 2)Now back to the formula you already know: = Round(100 * Pos / Max)Actually the main formula is something like this: = ( the_range * position / maximum_allowed ) + starting_valueIf you want to have -100.0.100 in the result, change the values in the lines #33..#34, and run the code. Now look at the line #50, you can see why the_range becomes ResultMax-ResultMin. And because you want the -100 as the starting_value, you have to do +ResultMin as you see in the line #52.About the div in the formula. Div runs faster than /. And div always gives integer result, so you don't have to do Round. ---------------------------------------------------------------------- Pe3s · 2023-12-03 09:03 @Handoko, I have one more question regarding the onMouseDown procedure, how can I modify the code to move the dot on mouse click? Change the code on line 46 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 Image2.Left := X;does not place the dot in the correct position ---------------------------------------------------------------------- Handoko · 2023-12-03 09:19 Add this Image1.OnClick event: 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.Image1Click(Sender: TObject);var  NewPos:TPoint;begin  NewPos := ScreenToClient(Mouse.CursorPos);  Image2.Left := NewPos.X - (Image2.Width div 2);end; You can get the mouse pointer position by using Mouse.CursorPos. But before you can use that values, you need to convert it by using ScreenToClient function. ---------------------------------------------------------------------- Pe3s · 2023-12-03 09:30 @Handoko, I am grateful for your help. Thank you Greetings with a smile ---------------------------------------------------------------------- Pe3s · 2023-12-03 11:47 @Handoko, I would also like to ask about automatic ball movement 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 Image2.Left := image1.Width * POS div ResultMax; ---------------------------------------------------------------------- Handoko · 2023-12-03 13:41 Not sure what did you mean. Maybe something like this: 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  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Image1: TImage;    Image2: TImage;    Label1: TLabel;    lbeGoto: TLabeledEdit;    lbeResultMax: TLabeledEdit;    lbeResultMin: TLabeledEdit;    procedure FormCreate(Sender: TObject);    procedure Image1Click(Sender: TObject);    procedure Image2MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer    );    procedure lbeGotoEditingDone(Sender: TObject);    procedure lbeResultMaxEditingDone(Sender: TObject);    procedure lbeResultMinEditingDone(Sender: TObject);  private    FResultMin: Integer;    FResultMax: Integer;    procedure CalculatePosition;    procedure GotoPosition(Pos: Integer);  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.lbeResultMaxEditingDone(Sender: TObject);var  NewValue: Integer;begin  if not(TryStrToInt(lbeResultMax.Text, NewValue)) then  begin    lbeResultMax.Text := IntToStr(FResultMax);    Exit;  end;  if NewValue <= FResultMin then  begin    lbeResultMax.Text := IntToStr(FResultMax);    Exit;  end;  FResultMax := NewValue;  CalculatePosition;end; procedure TForm1.lbeResultMinEditingDone(Sender: TObject);var  NewValue: Integer;begin  if not(TryStrToInt(lbeResultMin.Text, NewValue)) then  begin    lbeResultMin.Text := IntToStr(FResultMin);    Exit;  end;  if NewValue >= FResultMax then  begin    lbeResultMin.Text := IntToStr(FResultMin);    Exit;  end;  FResultMin := NewValue;  CalculatePosition;end; procedure TForm1.lbeGotoEditingDone(Sender: TObject);var  NewValue: Integer;begin  if not(TryStrToInt(lbeGoto.Text, NewValue)) then Exit;  if (NewValue < FResultMin) or (NewValue > FResultMax) then Exit;  lbeGoto.Text := '';  GotoPosition(NewValue);end; procedure TForm1.Image1Click(Sender: TObject);var  NewPos:TPoint;begin  NewPos      := ScreenToClient(Mouse.CursorPos);  Image2.Left := NewPos.X - (Image2.Width div 2);  CalculatePosition;end; procedure TForm1.Image2MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);var  AllowedMin,  AllowedMax : Integer;begin   if not(ssLeft in Shift) then Exit;   AllowedMin := Image1.Left;  AllowedMax := Image1.Left + Image1.Width - Image2.Width;   Image2.Left := Image2.Left + X - (Image2.Width div 2);  if Image2.Left < AllowedMin then Image2.Left := AllowedMin;  if Image2.Left > AllowedMax then Image2.Left := AllowedMax;   CalculatePosition; end; procedure TForm1.FormCreate(Sender: TObject);begin  FResultMin := StrToInt(lbeResultMin.Text);  FResultMax := StrToInt(lbeResultMax.Text);end; procedure TForm1.CalculatePosition;var  Result: Integer;begin  Result := ((FResultMax-FResultMin) *            (Image2.Left-Image1.Left) div          (Image1.Width-Image2.Width)) + FResultMin;  Label1.Caption := Result.ToString;end; procedure TForm1.GotoPosition(Pos: Integer);begin  Image2.Left := Round((Pos-FResultMin) * (Image1.Width- Image2.Width) /                 (FResultMax-FResultMin) + Image1.Left);  Label1.Caption := Pos.ToString;end; end. ---------------------------------------------------------------------- Pe3s · 2023-12-03 15:45 I meant how to calculate the progress of the bar with automatic progress, e.g. time elapsed, as is the case in players. 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 Image2.Left := AParam * (Image1.Width -image2.Width div 2) div ResultMax; AParam(current position) the above line of code counts the time correctly, but at startup it goes back to the left side, so I would like to correct it I am attaching a screenshot video of the bar ---------------------------------------------------------------------- Handoko · 2023-12-03 15:57 My code has: procedure GotoPosition(Pos: Integer); Have you tried it? Add a TTimer, set its Interval to 100 and add this TTimer.OnTimer event: 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.Timer1Timer(Sender: TObject);const  Progress: Integer = 0;begin  Inc(Progress);  if Progress > FResultMax then Progress := FResultMin;  GotoPosition(Progress);end; ---------------------------------------------------------------------- Pe3s · 2023-12-03 16:22 Unfortunately it does not work. I keep searching From procedure procedure GotoPosition(Pos: Integer); the bar does not move ---------------------------------------------------------------------- Handoko · 2023-12-03 16:29 I don't why it didn't work on your system. It works correctly on my Linux computer. 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  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Image1: TImage;    Image2: TImage;    Label1: TLabel;    lbeGoto: TLabeledEdit;    lbeResultMax: TLabeledEdit;    lbeResultMin: TLabeledEdit;    Timer1: TTimer;    procedure FormCreate(Sender: TObject);    procedure Image1Click(Sender: TObject);    procedure Image2MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer      );    procedure lbeGotoEditingDone(Sender: TObject);    procedure lbeResultMaxEditingDone(Sender: TObject);    procedure lbeResultMinEditingDone(Sender: TObject);    procedure Timer1Timer(Sender: TObject);  private    FResultMin: Integer;    FResultMax: Integer;    procedure CalculatePosition;    procedure GotoPosition(Pos: Integer);  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.lbeResultMaxEditingDone(Sender: TObject);var  NewValue: Integer;begin  if not(TryStrToInt(lbeResultMax.Text, NewValue)) then  begin    lbeResultMax.Text := IntToStr(FResultMax);    Exit;  end;  if NewValue <= FResultMin then  begin    lbeResultMax.Text := IntToStr(FResultMax);    Exit;  end;  FResultMax := NewValue;  CalculatePosition;end; procedure TForm1.lbeResultMinEditingDone(Sender: TObject);var  NewValue: Integer;begin  if not(TryStrToInt(lbeResultMin.Text, NewValue)) then  begin    lbeResultMin.Text := IntToStr(FResultMin);    Exit;  end;  if NewValue >= FResultMax then  begin    lbeResultMin.Text := IntToStr(FResultMin);    Exit;  end;  FResultMin := NewValue;  CalculatePosition;end; procedure TForm1.Timer1Timer(Sender: TObject);const  Progress: Integer = 0;begin  Inc(Progress);  if Progress > FResultMax then Progress := FResultMin;  GotoPosition(Progress);end; procedure TForm1.lbeGotoEditingDone(Sender: TObject);var  NewValue: Integer;begin  if not(TryStrToInt(lbeGoto.Text, NewValue)) then Exit;  if (NewValue < FResultMin) or (NewValue > FResultMax) then Exit;  lbeGoto.Text := '';  GotoPosition(NewValue);end; procedure TForm1.Image1Click(Sender: TObject);var  NewPos:TPoint;begin  NewPos    := ScreenToClient(Mouse.CursorPos);  Image2.Left := NewPos.X - (Image2.Width div 2);  CalculatePosition;end; procedure TForm1.Image2MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);var  AllowedMin,  AllowedMax : Integer;begin   if not(ssLeft in Shift) then Exit;   AllowedMin := Image1.Left;  AllowedMax := Image1.Left + Image1.Width - Image2.Width;   Image2.Left := Image2.Left + X - (Image2.Width div 2);  if Image2.Left < AllowedMin then Image2.Left := AllowedMin;  if Image2.Left > AllowedMax then Image2.Left := AllowedMax;   CalculatePosition; end; procedure TForm1.FormCreate(Sender: TObject);begin  FResultMin := StrToInt(lbeResultMin.Text);  FResultMax := StrToInt(lbeResultMax.Text);end; procedure TForm1.CalculatePosition;var  Result: Integer;begin  Result := ((FResultMax-FResultMin) *            (Image2.Left-Image1.Left) div          (Image1.Width-Image2.Width)) + FResultMin;  Label1.Caption := Result.ToString;end; procedure TForm1.GotoPosition(Pos: Integer);begin  Image2.Left := Round((Pos-FResultMin) * (Image1.Width- Image2.Width) /                 (FResultMax-FResultMin) + Image1.Left);  Label1.Caption := Pos.ToString;end; end. ---------------------------------------------------------------------- Pe3s · 2023-12-03 16:46 I reinstalled Lazarus, it works. Thank you again and sorry for the inconvenience