Вниз
Скачать: CL | DM;

Помогите, пожалуйста, оптимизировать заполнение ComboBox-ов   Найти похожие ветки 

← →
Федор   (2008-03-16 10:29) [0]

Помогите оптимизировать заполнение 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;

После нескольких попыток получилось вот это...
Может есть более практичный вариант?


← →
Kolan ©   (2008-03-16 10:41) [1]

Во превых можно и нужно уменьшить кол-во кода и сделать его более понятным.

Тут пахнет Duplicate Cod"ом.

Интерфейс примерно такой:
procedure FillCombo(ADataSet: TDataSet; AComboBox: TComboBox);


← →
Kolan ©   (2008-03-16 10:42) [2]

Еще используй ComboBox.Items.BeginUpdate и ComboBox.Items.EndUpdate.


← →
Kolan ©   (2008-03-16 10:46) [3]

> Интерфейс примерно такой:
> procedure FillCombo(ADataSet: TDataSet; AComboBox: TComboBox)
> ;

procedure FillCombo(ADataSet: TDataSet; AComboBox: TComboBox; AFieldIndex: Integer);


← →
Федор   (2008-03-16 10:49) [4]

хм...

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 11:00) [5]

> Ты что-то подобное имеешь ввиду?

Зачем запрос запихал туда?

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]


← →
Kolan ©   (2008-03-16 11:01) [6]

А на счет DataAware контролов ты не думал?


← →
Федор   (2008-03-16 11:21) [7]

Спасибо!
------------------------------------
и [2]                      - зачем?


← →
Kolan ©   (2008-03-16 17:00) [8]

> &#151; зачем?

Шоб быстрее было.


← →
ЦУП ©   (2008-03-16 17:21) [9]

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;


← →
ЦУП ©   (2008-03-16 17:22) [10]

Только вот тут опечатка:
if AComboBox.Items.IndexOf(Str)=-1 and Str<>"" then

Надо
if (AComboBox.Items.IndexOf(Str)=-1) and (Str<>"") then



Страницы: 1 вся ветка

Скачать: CL | DM;



Память: 0.47 MB
Время: 0.01 c
15-1204147672
Tirael
2008-02-28 00:27
2008.04.13
тем, кому не все равно


15-1204167411
Slider007
2008-02-28 05:56
2008.04.13
С днем рождения ! 28 февраля 2008 четверг


15-1202564961
Kerk
2008-02-09 16:49
2008.04.13
Проверялка занятости доменов


2-1205767970
webpauk
2008-03-17 18:32
2008.04.13
Проблемы с TlistView


15-1203931152
Макс12345
2008-02-25 12:19
2008.04.13
Ударения в словах.




   Наверх