如,combobox.item 中有下例的下拉项,4
                                    5
                                    4
                                    6
                                    7
                                    1
                                    6
                                    
要把其中重复的删除,(4,6)且删除后不是空行,急急急,
 在线等后,

解决方案 »

  1.   

    这个容易,可以采用分组
    with dataset1 do
    begin
         close;
          sql.Clear ;
          sql.Add('select  field1 from table1 group by field1);
          open;
          first;
          while not eof do
          begin
           combobox1.Items.Add(fieldbyname('field1').AsString );
           next;
          end;
    end;
    也可以第一句:select distinct field1 from table1
      

  2.   

    用ComboBox.Items.IndexOf()判断,为-1则添加
      

  3.   

    不是这样的呀,我的一个字段里如:4,5
                                    3
                                    4 ,6
                                    等等。
    也就是一个里放多条数据,我读到,combobox.item  里,变成了,4
                                                               5
                                                               3
                                                               4
                                                               6
           我第一次从库里也是用 ,DISTINCT ,
      

  4.   

    在ComboBox.Items.Add的时候采用如下语法:var item: string;if ComboBox.Items.IndexOf(item) < 0 then
      ComboBox.Items.Add(item);在添加Item的时候先判断一下值是否存在
      

  5.   

    如果Item已加,以下代码是删除的代码,已调试通过:procedure TForm1.BitBtn1Click(Sender: TObject);
    var I,J: Integer;
    begin
      for I := 0 to self.ComboBox1.Items.Count - 1 do
      begin
        for J := I+1 to self.ComboBox1.Items.Count - 1 do
        begin
          if (self.ComboBox1.Items[J] = self.ComboBox1.Items[I]) then
            self.ComboBox1.Items.Delete(J);
        end;
      end;
    end;
      

  6.   

    先排序再去重复比较快:
    Var
      Strs: TStringList
      Prev: String;
      I: Integer;
    begin
      Strs := TStringList.Create;
      Strs.Text = ComboBox1.Items.Text;
      Strs.Sort;
      ComboBox1.Items.Clear;
      Prev := '';
      for I := 0 to Strs.Count - 1 do
      begin
        if not AnsiSameStr(Prev, Strs[I]) then
        begin
          Prev := Strs[I];
          ComboBox1.Items.Add(Prev);
        end;
      end;
    end;
      

  7.   

    如果要求保持顺序会慢一些:
    Var
      Strs: TStringList
      I: Integer;
    begin
      Strs := TStringList.Create;
      Strs.Text = ComboBox1.Items.Text;
      ComboBox1.Items.Clear;
      for I := 0 to Strs.Count - 1 do
      begin
        if ComboBox1.IndexOf(Strs[I]) <> -1 then
        begin
          ComboBox1.Items.Add(Strs[I]);
        end;
      end;
    end;
      

  8.   

    combobox.items.clear;
    Query.close;
    query.sql.text:='Select distinct Field1 from table1 where...';
    query.open;
    while not query.eof do
    begin
      combobox.items.add(query.FindField('Field1').AsString);
      query.next;
    end;
      

  9.   

    DreamStrat(梦启动的摇篮…)   的方法和我开始想方法的差不多,
    plainsong(短歌) 的方法也可行。
    已经解决了,所以还是给分,每人10分,不好意思,分太少,
      

  10.   

    // 这是排好序的var
      L: TStringList;
      I: Integer;
    begin
      L := TStringList.Create;
      with L do
      try
        Duplicates := dupIgnore;
        Sorted := True;
        Assign(ListBox1.Items);
        ListBox1.Items.Clear;
        ListBox1.Items.Assign(L);
      finally
        Free;
      end;