现有ListBox1和ListBox2,ListBox1和ListBox2中已经有若干个值了(它们的值是一样的),我现在随机通过勾选来选中其中的5个值,把这5个值放进一个字符串变量str中,并用逗号隔开,就像这样:1,3,6,10,13 现在问题是这样的。怎样把我选定的值在ListBox2中相对应的Checked赋值成True?procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
str :string;
begin
//我用的是第三方控件中的ListBox  
for i:=0 to TeThemeListBox1.Items.Count-1 do
  begin
    if TeThemeListBox1.State[i] = cbChecked then
    str := str+TeThemeListBox1.Items.Strings[i]+',';
  end;
//在这里写代码,怎样把str拆成相应的值,并使TeThemeListBox2中对应值的Checked赋成True
end;

解决方案 »

  1.   

    循环定位","Pos(',', Str) > //定位,取值后Delete(Str, 1, 1);
      

  2.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
    i: integer;
    str :string;
    k: Integer;
    begin
    //我用的是第三方控件中的ListBox  
    for i:=0 to TeThemeListBox1.Items.Count-1 do
      begin
        if TeThemeListBox1.State[i] = cbChecked then
        Begin
          for k:=0 to TeThemeListBox2.Items.Count-1 do
            if TeThemeListBox2.Items.Strings[k] = TeThemeListBox1.Items.Strings[i] Then
        TeThemeListBox2.State[k] := cbChecked; 
       End; 
        end;
    end;
      

  3.   

    crossbow(La Vida Es Amor)的做法不错
      

  4.   

    对,使用 pos定位旧可以了!
      

  5.   

    whqcfp(whqcfp) :请问怎么截取str?因为ListBox1和ListBox2不在同一界面,要用一个str字符串来传递,最主要的问题就是怎么把str拆分开!我想的是把1,2,3,4,5,6这样的字符串拆成  1 2 3 4 5 6这样的单一字符串并放入TStringList中。能给点代码吗?
      

  6.   

    你用AnsiReplaceStr,把','换成回车符(#13#10),就可以了
      

  7.   

    直接用TSTRINGS存不就没有这个问题了吗
      

  8.   

    不用这么麻烦吧?在你点选 ListBox1 时,直接修改 ListBox2 不行吗?