![]() |
| ||
|
|
Новости |
Поиск |
FAQ |
Статьи |
Книги |
Новости VCL
| | Форумы ЧАТ |
Орешник |
|
| |||
| |||
« предыдущая ветвь | форум | следующая ветвь »
Помогите, пожалуйста, оптимизировать заполнение ComboBox-ов
Федор © (2008-03-16 10:29:00)Помогите оптимизировать заполнение ComboBox-ов:
if ADOQuery1.Active then ADOQuery1.Close;
ADOQuery1.SQL.Text := 'SELECT DISTINCT P1, P2, P3 FROM Base '+
'ORDER BY 1,2,3';
ADOQuery1.Open;
while not ADOQuery1.EOF do
begin
if ComboBox1.Items.IndexOf(ADOQuery1.Fields[0].asString)=-1
then begin
if ADOQuery1.Fields[0].asString<>''
then ComboBox1.Items.Add(ADOQuery1.Fields[0].asString);
end;
if ComboBox2.Items.IndexOf(ADOQuery1.Fields[1].asString)=-1
then begin
if ADOQuery1.Fields[1].asString<>''
then ComboBox2.Items.Add(ADOQuery1.Fields[1].asString);
end;
if ComboBox3.Items.IndexOf(ADOQuery1.Fields[2].asString)=-1
then begin
if ADOQuery1.Fields[2].asString<>''
then ComboBox3.Items.Add(ADOQuery1.Fields[2].asString);
end;
ADOQuery1.Next;
end;
После нескольких попыток получилось вот это...
Может есть более практичный вариант?
ЦУП (2008-03-16 17:22:00) [1]Только вот тут опечатка:
if AComboBox.Items.IndexOf(Str)=-1 and Str<>'' then
Надо
if (AComboBox.Items.IndexOf(Str)=-1) and (Str<>'') then
ЦУП (2008-03-16 17:21:00) [2]
procedure FillCombo(AComboBox: TComboBox; const Str: String);
begin
if AComboBox.Items.IndexOf(Str)=-1 and Str<>'' then
AComboBox.Items.Add(ADataSet.Fields[AFieldIndex].asString);
end;
while not ADOQuery1.EOF do
begin
FillCombo(ComboBox1,ADOQuery1.Fields[0].asString);
FillCombo(ComboBox2,ADOQuery1.Fields[0].asString);
FillCombo(ComboBox3,ADOQuery1.Fields[0].asString);
ADOQuery1.Next;
end;
Kolan (2008-03-16 17:00:00) [3]> зачем?
Шоб быстрее было.
Федор (2008-03-16 11:21:00) [4]Спасибо!
------------------------------------
и [2] - зачем?
Kolan (2008-03-16 11:01:00) [5]А на счет DataAware контролов ты не думал?
Kolan (2008-03-16 11:00:00) [6]> Ты что-то подобное имеешь ввиду?
Зачем запрос запихал туда?
if ADataSet.Active then ADataSet.Close;
ADataSet.SQL.Text := 'SELECT DISTINCT P1, P2, P3 FROM Base '+
'ORDER BY 1,2,3';
ADataSet.Open;
procedure FillCombo(ADataSet: TDataSet; AComboBox: TComboBox; AFieldIndex: Integer);
begin
while not ADataSet.EOF do
begin
if AComboBox.Items.IndexOf(ADataSet.Fields[AFieldIndex].asString)=-1
then
begin
if ADataSet.Fields[AFieldIndex].asString<>'' then
AComboBox.Items.Add(ADataSet.Fields[AFieldIndex].asString);
end;
ADataSet.Next;
end;
end;
Нехватает проверок и [2]
Федор (2008-03-16 10:49:00) [7]хм...
procedure FillCombo(ADataSet: TDataSet; AComboBox: TComboBox; AFieldIndex: Integer);
if ADataSet.Active then ADataSet.Close;
ADataSet.SQL.Text := 'SELECT DISTINCT P1, P2, P3 FROM Base '+
'ORDER BY 1,2,3';
ADataSet.Open;
while not ADataSet.EOF do
begin
if AComboBox.Items.IndexOf(ADataSet.Fields[AFieldIndex].asString)=-1
then begin
if ADataSet.Fields[AFieldIndex].asString<>''
then AComboBox.Items.Add(ADataSet.Fields[AFieldIndex].asString);
end;
ADataSet.Next;
end;
Ты что-то подобное имеешь ввиду?
Kolan (2008-03-16 10:46:00) [8]> Интерфейс примерно такой:
> procedure FillCombo(ADataSet: TDataSet; AComboBox: TComboBox)
> ;
procedure FillCombo(ADataSet: TDataSet; AComboBox: TComboBox; AFieldIndex: Integer);
Kolan (2008-03-16 10:42:00) [9]Еще используй
ComboBox.Items.BeginUpdateиComboBox.Items.EndUpdate.
Kolan (2008-03-16 10:41:00) [10]Во превых можно и нужно уменьшить кол-во кода и сделать его более понятным.
Тут пахнет Duplicate Cod'ом.
Интерфейс примерно такой:
procedure FillCombo(ADataSet: TDataSet; AComboBox: TComboBox);