我把数据库中查询的内容自动添加到combobox下拉列表中,由于按照小类查询的,写到下拉列表中的是大类的名称,所以出现了相同的大类名称。有办法去掉下拉列表中相同的内容吗?

解决方案 »

  1.   


    判断是否含有某项
    ComboBox1.Items.IndexOf('这里是某项的字符串')
      

  2.   

    数据库查询的时候,你加上distinct关键字不行么?
      

  3.   


     
    with ADOQuery do
             begin
                close;
                sql.Clear;
                sql.Add(' select distinct  字段 from 表名');
                Open;
                comboBox1.Items.Clear;
                first;
                while not eof do
                   begin
                      comboBox1.Items.Append(trim(fieldByName('字段').AsString));
                      next;
                   end;
               end;
        end
      

  4.   

    添加了什么就是什么,滤不掉要在查询时过滤掉: 
    select distinct A from tb或者添加时,判断是否已经存在,不存就添加:
    s:=AdoQuery.FieldByName('A').Asstring;
    if ComboBox1.Items.IndexOf(s)=-1 then
      ComboBox1.Items.Add(s);
      

  5.   

    sql查询的时候,先select distinct  字段 from 表名,再帮定到combobox上,comboBox1.Items.Append(trim(fieldByName('字段').AsString)
      

  6.   

    //自己写个函数
    procedure TForm1.addCombobox(s: String);
    begin
    //判断ITEMS中s这个字符存在于第几行,如果返回-1,则表示不存在
      if combobox1.Items.IndexOf(s) = -1 then
      begin
        combobox1.Items.Add(s);
      end;
    end;//要在COMBOBOX里添加时,调用这个函数procedure TForm1.Button1Click(Sender: TObject);
    begin
      addCombobox(edit1.text);//传递你要添加的字符
    end;
      

  7.   

    楼主可以利用TStringList对象的Duplicates属性实现你的要求。var
     s: TStringList;
    begin
      s := TStringList.Create;
      try
        s.Duplicates := dupIgnore;
        s.Add('example');
        self.ComboBox1.Items.Assign(s);
      finally
        s.Free;
      end;
      

  8.   

    方法很多,可以在数据查询时使用distinct或都继承TComboBoxStrings类并重写 Add和Insert方法
      

  9.   


    s.Add('example'); 换成你要添加内容的循环体即可。
      

  10.   

    对了,楼上chris_mao想法是好的,但Item并不是用TStringList创建的,所以只有自己实现了。呵呵