2015-10-24 33 views
0

我在Delphi7中的一個小應用程序工作,但是如何使用3個複選框的組合難題 例如:如果check.box1和checkbox2選中運行系統應用程序與這些prefs 或checkbox1僅檢查運行系統應用程序與這些偏好或如果checkbox2只檢查運行與這些偏好德爾福7:如何使用複選框的組合

+0

'if CheckBox.Checked then' –

+0

你需要試圖以一種概括你想要達到的內容的方式重寫你的問題。目前很難知道你在做什麼。 –

回答

2

通過MartynA的解決方案的啓發,因爲我比較喜歡緊湊的代碼,這裏是我會怎麼做:

// For the three (sets of?) preferences, define bit values, and 
// a variable to hold the combination of the selected preferences: 
const 
    PrefA=1; PrefB=2; PrefC=4; 
var 
    prefs: byte; 

procedure TForm12.Button2Click(Sender: TObject); 
begin 
    // whether a preference should be active or not becomes a simple `OR` function 
    // based on each checkbox's state 
    prefs := 0; 
    if CheckBox1.Checked then prefs := prefs or PrefA; 
    if CheckBox2.Checked then prefs := prefs or PrefB; 
    if CheckBox3.Checked then prefs := prefs or PrefC; 

    // then use a `case` statement to run according the preference combination 
    // The values like `PrefA or PrefC` can be used instead of numeric values, 
    // since they can be resolved at compile time. 
    // Whether they improve readability or not is ... (disputable) 

    case prefs of 
    0: 
     ShowMessage('Running without preferences'); 
    PrefA: 
     ShowMessage('Running with PrefA alone'); 
    PrefB: 
     ShowMessage('Running with PrefB alone'); 
    PrefA or PrefB: 
     ShowMessage('Running with PrefA and PrefB'); 
    PrefC: 
     ShowMessage('Running with PrefC alone'); 
    PrefA or PrefC: 
     ShowMessage('Running with PrefA and PrefC'); 
    PrefB or PrefC: 
     ShowMessage('Running with PrefB and PrefC'); 
    PrefA or PrefB or PrefC: 
     ShowMessage('Running with PrefA and PrefB and PrefC'); 
    end; 
end; 

如果你需要在你的代碼,以檢查是否偏好是否有效,你可以這樣做:

if (prefs and PrefB) then 
    // do whatever requires PrefB to be selected 
+0

個人而言,我會做類似你的事情,所以從這裏+1。我按照我的方式寫了我的,因爲我得到了OP難以掌握問題基本知識的印象。 – MartynA

+0

@MartynA是的,我認爲你做到了,事實上,你已經儘可能地理解了,因此我的+1。我只是想提出一種替代方案來剔除OP。 –

3

這聽起來像你有麻煩開始寫你需要什麼,這基本上是一系列的if ..那麼......根據複合布爾表達式來使用流控制語句。你可能想要一個像下面那樣的結構。我不會告訴你整個事情,因爲它是最適合你的工作是爲自己,但是這應該給你的想法:

if (CheckBox1.Checked) and (CheckBox2.Checked) and not (CheckBox3.Checked) then 
    begin 
     // do one thing 
    end 
    else 
    begin 
     if (CheckBox1.Checked) and not (CheckBox2.Checked) and not (CheckBox3.Checked) then 
     begin 
      // do another thing 
     end 
     else 
     // etc 
    end; 

如果你熟悉枚舉類型,你可能最好宣佈一個並使用它來推導出所選的偏好,然後對其進行處理。這樣做的目的是雙重的:增加代碼的清晰度(當然代價是代碼更長);並且將用於計算用戶已經從作用於其上的代碼中選擇哪個偏好的邏輯分開。

喜歡的東西

type 
    TRunPreference = (rpNone, rpOne, rpTwo, rpThree, rpFour, 
    rpFive, rpSix, rpSeven, rpEight); 

    // rpOne..rpEight are for the 8 permutations of three booleans 
    // and the rpNone is to provide for initialising the variable 
    // with something which isn't one of the desired values 

var 
    RunPreference : TRunPreference; 

procedure TForm1.Button1Click(Sender: TObject); 
begin 
    RunPreference := rpNone; 

    if (CheckBox1.Checked) and (CheckBox2.Checked) and not (CheckBox3.Checked) then 
    begin 
     RunPreference := rpOne; 
    end 
    else 
    begin 
     if (CheckBox1.Checked) and not (CheckBox2.Checked) and not (CheckBox3.Checked) then 
     begin 
      RunPreference := rpTwo; 
     end 
     else 
     ; // 
    end; 
    case RunPreference of 
    rpNone : 
     ; // do nothing 
    rpOne : 
     begin 
     // Act on rpOne 
     end; 
    rpTwo : 
     begin 
     // Act on rpTwo 
     end; 
    // etc 
    end; { Case of RunPreference } 
end; 
+0

非常感謝第一個答案解決了我的問題像一個魅力是新的德爾福長期以來一直使用批處理來處理我的自動化任務 –

+0

還有一個問題我如何主題了我的應用程序在德爾福7感謝 –

+1

你需要開始一個新的問問題 - 這是一個不同的話題。 – MartynA