2012-05-16 14 views
6

मैं जानना चाहता हूं कि trackbar1.position की विपरीत दिशा में अपना दूसरा trackbar.position दर्पण कैसे बनाना है। उदाहरण के लिए। 1 से 100दो ट्रैकबार दर्पण

तो जब 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; 

वैकल्पिक शुरू (मुझ से केन व्हाइट और टिप्पणियों ने सुझाव दिया; ओ)):

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' का उपयोग किया है। .. यही कारण है कि; ओ) लेकिन मैं ** सटीक ** प्रश्न का उत्तर देने के लिए अद्यतन करता हूं; ओ) – Whiler

संबंधित मुद्दे