function add; var i: integer; begin adoquery1.open; for i := 1 to to 14 do begin (findcomponent('combobox'+inttostr(i)) as tcombobox).items.add(adoquery1.fieldbyname('').asstring); end; combobox15.items.add(''); combobox16.items.add(''); end;
for ii:= 1 to 14 do begin TComboBox.FindComponent('combobox'+IntToStr(ii)).Items.Add('123'); TComboBox.FindComponent('combobox'+IntToStr(ii)).Items.Add('456'); end;
procedure TForm1.FormCreate(Sender: TObject); var I: Integer; begin With AdoQuery do begin Open; while Not Eof do begin ComboBox1.Items.Add(FieldByName('Field1').AsString); Next; end; end; for I := 2 to 14 do TComboBox(FindComponent('ComboBox' + IntToStr(I))).Items.Assign (ComboBox1.Items); end;
1.先在设计时把要赋值的14个的tag设为1; var I: Integer; ls:TStrings; Temp: TComponent; begin ls:=TStrings.Create; try With AdoQuery do begin Open; while Not Eof do begin ls.Add(FieldByName('Field1').AsString); Next; end; close; end; For i:=0 to ComponentCount -1 do begin Temp := Components[I]; if (Temp is TComboBox) then if (Temp as TComboBox).tag=1 then (Temp as TComboBox).Items.Assign(ls); end; Finally ls.free; end;
procedure TForm1.Button4Click(Sender: TObject); var i :integer; begin ADOQuery1.First; while not ADOQuery1.Eof do begin ComboBox1.Items.Add(ADOQuery1.FieldByName('name').AsString); ADOQuery1.Next; end; for i:=0 to self.ComponentCount-1 do begin if Components[i]<>Combobox1 then begin if Components[i].ClassType = TComboBox then TComboBox(Components[i]).Items.Assign(ComboBox1.Items); end; end; end;
我的速度快,不管你有几多个ComboBox总一样的。 循环的次数又少!
简单而有效:procedure TForm1.Button4Click(Sender: TObject); var i :integer; begin ADOQuery1.Open; ADOQuery1.First; while not ADOQuery1.Eof do begin ComboBox1.Items.Add(ADOQuery1.FieldByName('name').AsString); ADOQuery1.Next; end;for i:= 2 to 14 do begin TComboBox(FindComponent('ComboBox' + IntToStr(I))).Items:=combobox1.items; end;
的items和combobox1一样。--------------------
另两个可不能加上述的值
--------------------
是什么意思?
var
i: integer;
begin
adoquery1.open;
for i := 1 to to 14 do
begin
(findcomponent('combobox'+inttostr(i)) as tcombobox).items.add(adoquery1.fieldbyname('').asstring);
end;
combobox15.items.add('');
combobox16.items.add('');
end;
begin
TComboBox.FindComponent('combobox'+IntToStr(ii)).Items.Add('123');
TComboBox.FindComponent('combobox'+IntToStr(ii)).Items.Add('456');
end;
http://expert.csdn.net/Expert/topic/1381/1381664.xml?temp=.9872248
var
I: Integer;
begin
With AdoQuery do
begin
Open;
while Not Eof do
begin
ComboBox1.Items.Add(FieldByName('Field1').AsString);
Next;
end;
end;
for I := 2 to 14 do
TComboBox(FindComponent('ComboBox' + IntToStr(I))).Items.Assign (ComboBox1.Items);
end;
var
I: Integer;
ls:TStrings;
Temp: TComponent;
begin
ls:=TStrings.Create;
try
With AdoQuery do
begin
Open;
while Not Eof do
begin
ls.Add(FieldByName('Field1').AsString);
Next;
end;
close;
end;
For i:=0 to ComponentCount -1 do
begin
Temp := Components[I];
if (Temp is TComboBox) then
if (Temp as TComboBox).tag=1 then
(Temp as TComboBox).Items.Assign(ls);
end;
Finally
ls.free;
end;
var i :integer;
begin
ADOQuery1.First;
while not ADOQuery1.Eof do
begin
ComboBox1.Items.Add(ADOQuery1.FieldByName('name').AsString);
ADOQuery1.Next;
end;
for i:=0 to self.ComponentCount-1 do
begin
if Components[i]<>Combobox1 then
begin
if Components[i].ClassType = TComboBox then
TComboBox(Components[i]).Items.Assign(ComboBox1.Items);
end;
end;
end;
循环的次数又少!
var i :integer;
begin
ADOQuery1.Open;
ADOQuery1.First;
while not ADOQuery1.Eof do
begin
ComboBox1.Items.Add(ADOQuery1.FieldByName('name').AsString);
ADOQuery1.Next;
end;for i:= 2 to 14 do
begin
TComboBox(FindComponent('ComboBox' + IntToStr(I))).Items:=combobox1.items;
end;