CheckListBox 中的多个值

Multiple value in CheckListBox

如何实现这个案例: 我有包含 20 个项目的 CheckListBox:症状 1、症状 2、..、症状 20。用户可以 select 多个症状。让我感到困惑的是,如何为每个症状赋予多个值。这是我的代码:

  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 动态化?

这是我检查索引 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

您没有获得预期结果的一个可能原因是您从未清除 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;

声明疾病列表和要匹配的常量字符串数组:

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

看来你想要一组匹配所有检查症状的疾病。最新的更新显示了如何做到这一点。