select DISTINCT 用户名 FROM 用户名 WHERE 用户名 IS NOT NULL AND 用户名<>''
用sql语句过滤好之后,再给combobox传,sql语句类似这样的select DISTINCT 字段名 FROM 表名 ,给你个简单的代码吧,你可以看看 Combobox1.Clear; with ADOQuery1 do begin Close; SQL.Clear; SQL.Add('select distinct jkzx from jkzxwh'); Open; First; while not ADOQuery1.Eof do begin Combobox1.Items.Add(FieldByName('jkzx').AsString); Next; end; end; Combobox1.ItemIndex:=0;
利用combobox1.itmeindex:=-1来判断当前text没有出现在itmes中,就可以添加,保证不重复. 代码这样写 if combobox1.ItemIndex=-1 then combobox1.Items.Add(combobox1.Text). 主要是看你想把这句代码放在什么事件中去写.比如onexit,onkeypress(比如打回车key=#13的时候),或者onclick.
var st1:TStrings; s1:String; begin st1.clear; s1:=...;//准备加入的值 if st1.indexof(s1)<>-1 then begin st1.add(s1); end; Combobox1.Items:=st1;end;
如果你是用aodquery提取数据的,那用这个: 例如是你向数据表user中提取'号码'字段,应该是这样 adoquery1.Close; adoquery1.SQL.Clear; adoquery1.SQL.Add('select distinct 号码 from user'); adoquery1.Open; while not adoquery1.Eof do begin combobox1.Items.Add(adoquery1.fieldbyname('号码').AsString); adoquery1.Next; end;
Combobox1.Clear;
with ADOQuery1 do begin
Close;
SQL.Clear;
SQL.Add('select distinct jkzx from jkzxwh');
Open;
First;
while not ADOQuery1.Eof do begin
Combobox1.Items.Add(FieldByName('jkzx').AsString);
Next; end;
end;
Combobox1.ItemIndex:=0;
代码这样写 if combobox1.ItemIndex=-1 then combobox1.Items.Add(combobox1.Text).
主要是看你想把这句代码放在什么事件中去写.比如onexit,onkeypress(比如打回车key=#13的时候),或者onclick.
遍历一遍 把 空的 和重复的去掉
需要用一个TListstrings 做一个备份 拿来比较去除 重复的
ComboBox1.Items.Add('');
if ComboBox1.Items.IndexOf('你的字符串') <0 then
ComboBox1.Items.Add('你的字符串'); 这个是对的
TStringList(ComboBox1.Items).Duplicates := dupIgnore; // 忽略重复的项目
st1:TStrings;
s1:String;
begin
st1.clear;
s1:=...;//准备加入的值
if st1.indexof(s1)<>-1 then
begin
st1.add(s1);
end; Combobox1.Items:=st1;end;
例如是你向数据表user中提取'号码'字段,应该是这样
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select distinct 号码 from user');
adoquery1.Open;
while not adoquery1.Eof do
begin
combobox1.Items.Add(adoquery1.fieldbyname('号码').AsString);
adoquery1.Next;
end;