Two TrackBar Mirrors

I would like to know how to make my second mirror trackbar.position in the opposite direction of trackbar1.position. eg. The range is from 1 to 100.

So, when TrackBar1.Position := 2 , then trackbar2.Position := 99 No matter how the trackballs go, I would like to flip in the opposite direction.

Here is my code so far: (not interested in using keys for this), just mouse interaction.

 Direction : string; Skip : boolean; procedure TForm1.TrackBar1Change(Sender: TObject); begin if TrackBar1.Position = TrackBar2.Position then begin if Direction = 'up' then TrackBar2.Position := TrackBar2.Position + 1; if Direction = 'down' then TrackBar2.Position := TrackBar2.Position - 1; skip := true; end; if TrackBar1.Position < TrackBar2.Position then begin if skip = false then begin TrackBar2.Position := TrackBar2.Position - 1; Direction := 'down'; end; end else begin if skip = false then begin TrackBar2.Position := TrackBar2.Position + 1; Direction := 'up'; end; end; end; 

I'm probably overdoing it. Maybe there is an easier way. I prefer a simpler way. Thanks,

Ben

+6
source share
1 answer

Two OnChange track OnChange are associated with this code:

 procedure TForm1.TrackBarChange(Sender: TObject); var tbSource, tbTarget: TTrackBar; begin if Sender = TrackBar1 then // Check the Trackbar which triggers the event begin tbSource := TrackBar1; tbTarget := TrackBar2; end else begin tbSource := TrackBar2; tbTarget := TrackBar1; end; tbTarget.OnChange := nil; // disable the event on the other trackbar tbTarget.Position := tbSource.Max + tbSource.Min - tbSource.Position; // set the position on the other trackbar tbTarget.OnChange := TrackBarChange; // define the event back to the other trackbar // Call a function or whatever after this line if you need to do something when it changes // lbl1.Caption := IntToStr(TrackBar1.Position); // lbl2.Caption := IntToStr(TrackBar2.Position); end; 

Alternative start (suggested by Ken White and comments from me; o)):

 procedure TForm1.TrackBarChange(Sender: TObject); var tbSource, tbTarget: TTrackBar; begin // if Sender is TTrackBar then // is it called 'from' a trackbar? // begin tbSource := TTrackBar(Sender); // Set the source if tbSource = TrackBar1 then // Check the Trackbar which triggers the event tbTarget := TrackBar2 else tbTarget := TrackBar1; tbTarget.OnChange := nil; // disable the event on the other trackbar tbTarget.Position := tbSource.Max + tbSource.Min - tbSource.Position; // set the position on the other trackbar tbTarget.OnChange := TrackBarChange; // define the event back to the other trackbar // Call a function or whatever after this line if you need to do something when it changes // lbl1.Caption := IntToStr(TrackBar1.Position); // lbl2.Caption := IntToStr(TrackBar2.Position); // end; end; 
+5
source

Source: https://habr.com/ru/post/915894/


All Articles