procedure TfrmDisplayConfig.SpeedButton1Click(Sender: TObject);
var iIndex, i: integer;
begin
  for i := 0 to ListBoxFields.Items.Count-1 do
  begin
    if ListBoxFields.Selected[i] then
    begin
      iIndex := ListBoxFields.ItemIndex;
      if ListBoxFields.ItemIndex >= 0 then
      begin
        ListboxDspFields.Items.Add(ListBoxFields.Items [ListBoxFields.ItemIndex]);
        ListboxFields.Items.Delete(ListBoxFields.ItemIndex);
        ListboxFields.ItemIndex := iIndex;
      end;
    end;
    if i=ListBoxFields.Items.Count-1 then Exit;
  end;
end;
将ListBoxFields中被选中的条目移到ListboxDspFields中,不知怎么总是有问题。
请大家看看。

解决方案 »

  1.   

    你在循环里用ListboxFields.Items.Delete(ListBoxFields.ItemIndex);把项目删除了,造成其它项的索引值变了。
      

  2.   

    procedure TfrmDisplayConfig.SpeedButton1Click(Sender: TObject);
    var iIndex, i: integer;
    begin
      for i := ListBoxFields.Items.Count-1  to 0 do
      begin
        if ListBoxFields.Selected[i] then
        begin
          iIndex := ListBoxFields.ItemIndex;
          if ListBoxFields.ItemIndex >= 0 then
          begin
            ListboxDspFields.Items.Add(ListBoxFields.Items [ListBoxFields.ItemIndex]);
            ListboxFields.Items.Delete(ListBoxFields.ItemIndex);
            ListboxFields.ItemIndex := iIndex;
          end;
        end;
        if i=ListBoxFields.Items.Count-1 then Exit;
      end;
    end;
      

  3.   

    把循环倒过来就行了:
    procedure TfrmDisplayConfig.SpeedButton1Click(Sender: TObject);
    var iIndex, i: integer;
    begin
      for i := ListBoxFields.Items.Count-1 downto 0 do
      begin
        if ListBoxFields.Selected[i] then
        begin
          ListboxDspFields.Items.Insert(0, ListBoxFields.Items[i]);  // 改为插入到第一项
          ListboxFields.Items.Delete(i);
        end;
        if i=ListBoxFields.Items.Count-1 then Exit;
      end;
    end;
      

  4.   

    sysu(死树) ( )
    我也意识到是这个问题了,但不知道怎么改?
      

  5.   

    谢谢死树和ghyghost,已经搞定了~