6

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

So When TrackBar1.Position := 2, then trackbar2.Position := 99 Regardless of which way the trackbars goes, I would like to mirror in the opposite direction.

Heres my code so far: (not interested in using keys to do 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;

Im probably overdoing this. Maybe there is a simpler way. I prefer the simpler way. Thanks,

Ben

Whiler
  • 7,998
  • 4
  • 32
  • 56
Ben
  • 565
  • 1
  • 3
  • 11

1 Answers1

5

The 2 trackbars OnChange events are linked to 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;
Whiler
  • 7,998
  • 4
  • 32
  • 56
  • 1
    `tbTo.Position := 100 - tbFrom.Position + 1;` (1-100,2-99) – teran May 16 '12 at 09:51
  • @teran: I have used `min := 0` & `max := 100`... this is why ;o) but I update to answer the **exact** question ;o) – Whiler May 16 '12 at 09:55
  • 2
    @Whiler :) Ben, note that up/down/left/right arrows work without any code – teran May 16 '12 at 09:58
  • 1
    @Ben: you're welcome (as you are new on StackOverflow... I explain, just in case... on left part of the answers/questions... there's a counter... to vote +1 if it's ok, -1, if it's not... and below, you can Accept an answer if it's what you are expecting...) – Whiler May 16 '12 at 09:59
  • 2
    +1. You can simplify your variable assignments, though: `tbSource := TTrackBar(Sender); if tbSource = TrackBar1 then tbTarget := TrackBar2 else tbTarget := TrackBar1;` – Ken White May 16 '12 at 22:19