我想在数据库中应用这样一个问题。因为我单位部门未确定,所以部门暂时不一定叫什么名字,所以在程序运行时,输入人员先在combobox中输入新建的部门,等以后在输入与先前输入相同的部门时,下拉条中就已经有了这个部门,如果没有,就新输入一个部门,同样这个新部门在再次输入时同样也在下拉表中找到。输入新的信息的前提是,以前没有输入过,下拉表中没有,只要输入过一次的信息,下拉表中都有相应的信息,而且信息是不重复的,如网络部只能有一个,再输入网络部,combobox中也不会加入这条已经存在的信息。
我用的是table,部门的字段名是post,能否写一些源代码,要是光说几句,小弟功力太浅,可能领悟不了。
求求前辈们多多赐教,我是个刚学DELPHI的菜鸟,希望前辈们多帮忙,小弟感激不尽。
                                                                 叩首

解决方案 »

  1.   

    好像Combobox是没有办法实现的
    因为Combobox是无法查找记录的  
    我感觉应该使用第三方控件了解决问题
    在CloseUP事件中判断如果有的话不能存储,没有则可以保存,然后刷新数据集。重新填写ComboBox中的数据。
      

  2.   

    procedure TForm2.ComboBox1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
      var
       i:integer;
       t:string;
    begin
     if key=13 then
      if (combobox1.Text <>'') then
      begin
         i:=0;
        repeat
         t:=combobox1.Items.Strings[i];
      if combobox1.Text=t then
        abort
        else
         begin
      combobox1.Items.Add(combobox1.Text);//把该处后面加上的表tabel进行的操作,就是把数据添加到表中的相应位置上!
      combobox1.Text:='';
      end;
       i:=i+1;
      until  i= combobox1.Items.Count
      end
      else
        showmessage('This Combox1 is empty!');end;
      

  3.   

    前面只是一个写入的程序段,在FORM.CREATE的时候你把表中数据读写到combobox1.item.add(Tabel1.fields);中!
      

  4.   

    调试通过,给分吧,就在combobox的keydown事件中写
    procedure TForm1.ComboBox1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    var i:integer;
        find:boolean;
    begin
       find:=false;
       if key=13 then
         begin
           for i:=0 to combobox1.Items.Count-1 do
             if  combobox1.Items[i]=combobox1.Text then find:=true;
           if find=false then combobox1.Items.Add(combobox1.Text);
         end;end;
      

  5.   

    在每次编辑新部门之前 查询并且重新添加列表
        再每次 保存之前检查是否有重复数据
    procedure TForm2.ComboBox1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
      var
       i:integer;
       t:string;
    begin
     if key=13 then
      if (combobox1.Text <>'') then
      begin
         i:=0;
        repeat
         t:=combobox1.Items.Strings[i];
      if combobox1.Text=t then
        abort
        else
         begin
      combobox1.Items.Add(combobox1.Text);//把该处后面加上的表tabel进行的操作,就是把数据添加到表中的相应位置上!
      combobox1.Text:='';
      end;
       i:=i+1;
      until  i= combobox1.Items.Count
      end
      else
        showmessage('This Combox1 is empty!');end;
      

  6.   

    建立一个动态数组,在formcreate时从数据库中读数据建立,然后根据这个动态数组建combobox.additem(),如果要建新部门时,查数组中有没相关的就行了,不用去查数据库,然后有新的部门加入的时候除了添加到数据库,也要添加上数组,把建combobox.additem弄成一个过程,需要更新时调用一次就行了