关于ComboBox (50分)

嫩手

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样判断ComboBox它的Item里已经有了已经赋给的值,如果没有, 我再添加时自动保存
比如:
Item里有
同学
同事
我在程序运行后,在ComboBox中再添加一个 ‘朋友’,它能够自动保存到Items里, 并能
够下次能在下拉中看到此item
 
procedure TForm1.Button1Click(Sender: TObject);
begin
ComboBox1.Items.Add(edit1.text);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
ComboBox1.Items.SaveToFile('ss.ss');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
ComboBox1.Items.LoadFromFile('ss.ss');
end;
 
在onchange事件里判断有没有,没有就存入文件里,下次调出。
 
对,存入文件。这是通常的做法,或者存如库里,不过好象没必要
 
存入数据库,文件或注册表
 
procedure TForm1.ComboBox1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key=VK_RETURN then
begin
if combobox1.items.indexof(combobox1.text)=-1 then
begin
combobox1.items.add(combobox1.text);
end;
end;
end;
 
判断是否存在输入的值,否则写入文件,刚才没事儿,我调试过了。
Var
s,t:string;
i:integer;
begin
s:=Combobox1.text;
t:='';
for i:=0 to combobox1.Items.Count-1 do
t:=t+Combobox1.Items;
if Pos(s,t)=0 then
begin
combobox1.Items.Add(s);
combobox1.Items.SaveToFile('c:/aa.txt'); //写入到文件以便下次调入
end;
end;
 
Sorry

Nstar 方法更实惠!!!
 
多人接受答案了。
 
顶部