Load items into a combobox from the select of another combobox

Asked

Viewed 390 times

2

I have a Combobox that lists school units coming from my comic. Follow the code:

_fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)   {
QueryUnidade->Close();
QueryUnidade->Open();

while (QueryUnidade->Eof == false){
   ComboBoxUn->Items->Add(QueryUnidade->FieldByName("unidade")->AsString);
   QueryUnidade->Next();
}

}

In my other combobox I need to list the Turns that are associated with the unit chosen in the first combobox, but it is not appearing. My code is like this:

void __fastcall TForm1::ComboBoxTurnoChange(TObject *Sender)    {

QueryTurno->Close();
QueryTurno->ClearFields();
QueryTurno->SQL->Add("SELECT DISTINCT TURNO FROM ALUNO WHERE UNIDADE ='"+  (Trim(ComboBoxUn->Text)+"'"));
QueryTurno->Open();

while(QueryTurno->Eof == false){
    ComboBoxTurno->Items->Add(QueryTurno->FieldByName("turno")->AsString);
   QueryTurno->Next();
}
 ComboBoxTurno->Update();
}

1 answer

0

The program didn’t work because the right procedure was to do the procedure shown in the Onselect of the first combobox (Comboboxun). The Code was as follows:

void __fastcall TForm1::ComboBoxUnSelect(TObject *Sender)
{
    QueryTurno->Close();
    QueryTurno->SQL->Add("SELECT DISTINCT TURNO FROM ALUNO WHERE UNIDADE="+QuotedStr(Trim(ComboBoxUn->Text)));
    QueryTurno->Open();

    while(QueryTurno->Eof == false){
       ComboBoxTurno->Items->Add(QueryTurno->FieldByName("turno")->AsString);
       QueryTurno->Next();
    }
}

This way when an option is selected in the first combobox automatically the other options are listed in the second.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.