2015-07-22 42 views
-1

如何實施此案例: 我有CheckListBox與20個項目:症狀1,症狀2,..,症狀20。用戶可以選擇多個症狀。讓我感到困惑的是,如何給每個症狀賦予多重價值。這裏是我的代碼:CheckListBox中的多個值

for i := 0 to CheckListBox1.Items.Count - 1 do 
    begin 
    if CheckListBox1.Checked[i] = True then 
    begin 
     Memo1.Lines.Append(CheckListBox1.Items.Strings[i]); 
     if i = 0 
     p1 := 'Disease 1'; 
     p2 := 'Disease 2'; 
     p3 := 'Disease 3'; 
     if i = 1 then 
     p1 := 'Disease 2'; 
     if i = 2 then 
     p1 := 'Disease 1'; 
     if i = 3 then 
     p1 := 'Disease 3'; 
     if i = 4 then 
     p1 := 'Disease 2'; 
     p2 := 'Disease 3'; 
     if i = 5 then 
     p1 := 'Disease 1'; 
     p2 := 'Disease 5'; 
     p3 := 'Disease 6'; 
     if i = 6 then 
     p1 := 'Disease 5'; 

     Memo1.Lines.Add('Disease:' + p1+', '+p2+', '+p3); 
     Memo1.Lines.Add(''); 
    end; 
    end; 
end; 

但結果並不如我所料。如何製作p1,p2,p3 dinamically?

下面是結果,當我在指數2,4,6檢查:

Symptomp 3 
Disease:Disease 1, Disease 5, Disease 6 

Symptomp 5 
Disease:Disease 2, Disease 5, Disease 6 

Symptomp 7 
Disease:Disease 5, Disease 5, Disease 6 
+0

輸出是代碼定義的內容。你想要發生什麼? –

+0

我想製作一個數組來保存每個疾病的p1,p2,p3值。 – Alca

+0

這不是很清楚,你能否舉幾個例子說明你想要得到什麼以及你實際得到了什麼? – Marco

回答

0

聲明的疾病名單和常量字符串數組來匹配:

// List of diseases 
type 
    // Note: Use descriptive names instead of a numbers 
    TDisease = (td1,td2,td3,{..,}tdMaxDiseases); 
    TDiseaseSet = set of TDisease; 

    TSymptom = (ts1,ts2,ts3,{..,}tsMaxSymptoms); 

const 
    // A list of disease names 
    sDisease: array[TDisease] of String = 
    ('Disease 1','Disease 2','Disease 3',{..,}'Disease xx'); 
    // An array of disease sets corresponding to each symptom 
    cMyDiseaseSet : array[TSymptom] of TDiseaseSet = ([td1,td2,td3],[td3],[td1],[td2]); 

設置的恆定陣列宣佈了一組疾病的各種症狀。


要獲得每個症狀將結果字符串和一組與疾病相匹配的症狀:

// A Function to retrieve the diseases from a symptom 
function DiseaseFromSymptom(aSymptom: TSymptom; var diseaseSet: TDiseaseSet): String; 
var 
    aDisease: TDisease; 
begin 
    diseaseSet := cMyDiseaseSet[aSymptom]; 
    Result := ''; 
    for aDisease in diseaseSet do 
    Result := Result + sDisease[aDisease] + ', '; 
    SetLength(Result,Length(Result)-2); 
end; 

var 
    diseases,diseasesSummary: TDiseaseSet; 
    s: String; 

    diseasesSummary := []; 
    for i := 0 to CheckListBox1.Items.Count - 1 do 
    begin 
    if CheckListBox1.Checked[i] = True then 
    begin 
     s := DiseaseFromSymptom(TSymptom(i),diseases); 
     Memo1.Lines.Append(CheckListBox1.Items.Strings[i]); 
     Memo1.Lines.Add('Disease:' + s); 
     Memo1.Lines.Add(''); 
     // Insert diseases 
     diseasesSummary := diseasesSummary + diseases; 
    end; 
    end; 
    // A complete set of diseases in diseasesSummary 

看來,只要你想匹配的一組疾病所有檢查的症狀。最新的更新顯示瞭如何做到這一點。

0

你沒有得到預期的結果一個可能的原因是,你永遠清除p1p2p3變量,因此,如果CheckListBox1.Checked[0]爲真,則將分配p2p3,則如果CheckListBox1.Checked[1]也爲真,那麼p2p3仍然具有來自先前迭代的值,而它們可能應該是空白的。嘗試類似

for i := 0 to CheckListBox1.Items.Count - 1 do 
    begin 
    if CheckListBox1.Checked[i] = True then 
    begin 
     p1 := ''; 
     p2 := ''; 
     p3 := ''; 

     Memo1.Lines.Append(CheckListBox1.Items.Strings[i]); 

     if i = 0 then begin 
     p1 := 'Disease 1'; 
     p2 := 'Disease 2'; 
     p3 := 'Disease 3'; 
     end; 
     ...  
     Memo1.Lines.Add('Disease:' + p1+', '+p2+', '+p3); 
     Memo1.Lines.Add(''); 
    end; 
    end; 
end; 
+0

它不起作用。 – Alca