2012-05-16 13 views
6

Mi piacerebbe sapere come rendere il mio secondo trackbar.posizionamento speculare nella direzione opposta di trackbar1.position. es. variare da 1 a 100.Specchio TrackBar due

Così, quando TrackBar1.Position := 2, quindi trackbar2.Position := 99 Indipendentemente dal modo in cui i trackbars va, vorrei riflettere nella direzione opposta.

Ecco il mio codice finora: (non interessa usare i tasti per fare questo), solo l'interazione del mouse.

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 probabilmente esagerando. Forse c'è un modo più semplice. Preferisco il modo più semplice. Grazie,

Ben

risposta

5

I 2 trackbars OnChange eventi sono legati a questo codice:

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; 

inizio alternativo (suggerito da Ken Bianco e commenti da 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; 
+1

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

+0

Grazie heap ragazzi – Ben

+0

@teran: ho usato 'min: = 0' &' max: = 100'. .. questo è il motivo per cui; o) ma aggiorno per rispondere alla domanda ** esatta ** o) – Whiler

Problemi correlati