用如下语句控制几个memo控件的关键背景颜色
if trim(ListView2.Items[ListView2.Selected.Index].SubItems.strings[8]) = '111100' then
  begin
    memo1.Color := clYellow;
    memo2.Color := clYellow;
    memo3.Color := clYellow;
    memo4.Color := clYellow;
    memo5.Color := clWhite;
    memo6.Color := clWhite;
  end;其中ListView2.Items[ListView2.Selected.Index].SubItems.strings[8]) = '111100'从一个listview中取值,
这个值是由0和1构成的一个六位数字,分别对应6个memo控件,6位数中是1的相应的memo显示黄色,是0显示白色.问题:现在不知道这6位0和1能有多少种组合,如何编写能适应这个值的变化而变换memo颜色的程序呢?请教,谢谢

解决方案 »

  1.   


    var
      I: Integer;
    begin
      for I := 1 to 6 do
        with TMemo(Self.FindComponent('Memo'+InttoStr(I))) do
          if trim(ListView2.Items[ListView2.Selected.Index].SubItems.strings[8])[I] = 0 then
            Color := clWhite else Color := clYellow;
    end;
      

  2.   

    0两边忘记写单引号了另:
    如果trim(ListView2.Items[ListView2.Selected.Index].SubItems.strings[8])[I]这种用法不合适,就用一个临时字符串:Str := trim(ListView2.Items[ListView2.Selected.Index].SubItems.strings[8]);
    ....
    if Str[I] = '0' then...没测试. -_-...
      

  3.   

    To:lihuasoft 
    不行啊!
    trim(ListView2.Items[ListView2.Selected.Index].SubItems.strings[8])[I] = '0';
    最后那个值不是0,而是一个六位数字组成的字符串,如:'000100' '010010'  '000001' 等,现在要将这6位数字的每一位对应一个memo来控制memo的颜色,是1的相应memo显示黄色,是0显示白色.
      

  4.   

    用了很多if 如下:
     if trim(ListView2.Items[ListView2.Selected.Index].SubItems.strings[8]) = '100000' then
      begin
        cxmemo7.Style.Color := clYellow;
        cxmemo8.Style.Color := clWhite;
        cxmemo9.Style.Color := clWhite;
        cxmemo10.Style.Color := clWhite;
        cxmemo11.Style.Color := clWhite;
        cxmemo12.Style.Color := clWhite;
      end;
      if trim(ListView2.Items[ListView2.Selected.Index].SubItems.strings[8]) = '110000' then
      begin
        cxmemo7.Style.Color := clYellow;
        cxmemo8.Style.Color := clYellow;
        cxmemo9.Style.Color := clWhite;
        cxmemo10.Style.Color := clWhite;
        cxmemo11.Style.Color := clWhite;
        cxmemo12.Style.Color := clWhite;
      end;
      if trim(ListView2.Items[ListView2.Selected.Index].SubItems.strings[8]) = '111000' then
      begin
        cxmemo7.Style.Color := clYellow;
        cxmemo8.Style.Color := clYellow;
        cxmemo9.Style.Color := clYellow;
        cxmemo10.Style.Color := clWhite;
        cxmemo11.Style.Color := clWhite;
        cxmemo12.Style.Color := clWhite;
      end;
      if trim(ListView2.Items[ListView2.Selected.Index].SubItems.strings[8]) = '111100' then
      begin
        cxmemo7.Style.Color := clYellow;
        cxmemo8.Style.Color := clYellow;
        cxmemo9.Style.Color := clYellow;
        cxmemo10.Style.Color := clYellow;
        cxmemo11.Style.Color := clWhite;
        cxmemo12.Style.Color := clWhite;
      end;................
    很明显不合适,请教解决方法
      

  5.   


    //不管你的是'111100',还是'010101' ....var
      tmpstr: string;
      i: integer;
    begin
      tmpstr:= trim(ListView2.Items[ListView2.Selected.Index].SubItems.strings[8]);  
      for i:= 1 to length(tmpstr) do
      begin
        case strtoint(copy(tmpstr,i,1)) of
          0:(FindComponent('memo'+IntToStr(i)) as Tmemo).Color := clWhite;
          1:(FindComponent('memo'+IntToStr(i)) as Tmemo).Color := clYellow;
        end;
      end;
    end;
      

  6.   


    您没有看懂俺的代码。您看到那个[I]以及那个for循环了吗?