2012-05-16 82 views
6

我想知道如何在trackbar1.position的相反方向創建我的第二個trackbar.position鏡像。 例如。 範圍從1到100兩個TrackBar鏡像

所以當TrackBar1.Position := 2,然後trackbar2.Position := 99 不管trackbars變爲哪種方式,我想在相反的方向,以反映。

繼承人我的代碼到目前爲止:(不感興趣的使用鍵來做到這一點),只是鼠標交互。

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; 

我可能過度這個。也許有一個更簡單的方法。我更喜歡更簡單的方式。 謝謝,

回答

5

的2個trackbars OnChange事件都與此代碼:

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; 

替代開始(從我肯白色和意見建議; 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; 
+1

'tbTo.Position:= 100 - tbFrom.Position + 1;'(1-100,2-99) – teran

+0

謝謝堆人 – Ben

+0

@teran:我已經使用了'min:= 0'&'max:= 100'。 ..這是爲什麼; o)但我更新回答**確切**問題; o) – Whiler