【问题标题】:Multiple value in CheckListBoxCheckListBox 中的多个值
【发布时间】:2015-07-22 07:21:02
【问题描述】:

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

  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

【问题讨论】:

  • 输出是代码定义的。你想发生什么?
  • 我想创建一个数组来保存每个疾病的 p1,p2,p3 值。
  • 不是很清楚,能不能举几个例子说明你想得到什么和实际得到什么?
  • @Marco 进行进一步处理,我需要根据检查的症状选择哪种疾病。我很困惑如何解释我的问题。 -_-
  • 我不太清楚你想要什么

标签: delphi checkbox multiple-value


【解决方案1】:

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

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

set 常量数组为每个症状声明一组疾病。


要获得每个症状的结果字符串和与症状匹配的疾病集合:

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

您似乎想要一组与所有检查症状相匹配的疾病。最新更新显示了如何做到这一点。

【讨论】:

    【解决方案2】:

    您没有得到预期结果的一个可能原因是您从未清除过p1、p2 和p3 变量,因此如果CheckListBox1.Checked[0] 为真,那么将分配p2 和p3 ,那么如果CheckListBox1.Checked[1] 也为真,那么p2 和p3 仍将具有上一次迭代的值,而它们可能应该为空。尝试类似

    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;
    

    【讨论】:

    • 没有效果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多