-1

How to implement this case: I have CheckListBox with 20 items: Symptomp 1, Symptomp 2,.., Symptomp 20. User can select more than one symptomp. Something that make me confuse is, how to give multiple value for each of symptomp. Here is my code:

  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;

But the result is not as i expected. How to make p1,p2,p3 dinamically ?

Here is the result when i check at index 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
Alca
  • 81
  • 2
  • 13

2 Answers2

0

One possible reason you don't get expected result is that you never clear the p1, p2 and p3 variables, so if CheckListBox1.Checked[0] is true then p2 and p3 will be assigned, then if CheckListBox1.Checked[1] is true too then p2 and p3will still have a value from previous iteration while they probably should be blank. Try something like

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;
ain
  • 22,394
  • 3
  • 54
  • 74
0

Declare a list of diseases and a constant string array to match:

// 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]);

The set constant array declares a set of diseases for each symptom.


To get a resulting string for each symptom and a set with diseases matching the symptom:

// 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

It seems as you want a set of diseases matching all checked symptoms. The latest update shows how to do that.

LU RD
  • 34,438
  • 5
  • 88
  • 296