Change one Lookupcombobox according to another

Asked

Viewed 350 times

1

I’d like to make one LookUpCombobox in Delphi who behaved as follows:

  • When the user clicked choosing Brazilian or Brazilian, Born Abroad or Naturalized, other LookUpCombobox was automatically selected as the country Brazil and was unavailable for change.
  • When the user selected Foreigner, the other LookUpCombobox would be blank and the user would have the option to choose the country, but could not choose Brazil.

When I click on Post to save the values, the Parents field is blank and not saved when it is Brazil. I made a showmessage() in the variable and it returns the correct country code which is 76.

Follows the code:

procedure TformCadastroEstudantes.DBLookupComboBoxNacionalidadeCloseUp(
  Sender: TObject);
  var nacionalidade : Integer;
  var pais :   Integer;
  var enabled : Boolean;

  begin

    nacionalidade := DBLookupComboBoxNacionalidade.KeyValue;
    pais := DBLookupComboBoxPais.KeyValue;
    enabled := True;

    AtualizaPaises(nacionalidade, pais, enabled);

    DBLookupComboBoxPais.KeyValue := pais;
    DBLookupComboBoxPais.Enabled  := enabled;
  end;

Follow the code of procedure:

procedure AtualizaPaises(var nacionalidade : Integer; var pais: Integer; var enabled : Boolean);

begin

     if ((nacionalidade = 1) OR (nacionalidade = 2)) then
     begin
       pais := 76;
       enabled := False;

       with dm.sqlPaises do
       begin
         Close;
         SQL.Clear;
         SQL.Add('select * from PAISES');
         SQL.Add('where CODIGO_PAIS like ''76''');
         SQL.Add('order by NOME_PAIS');
         Open;
       end;

     end

    else
    begin
       pais := 0;
       enabled := True;

       with dm.sqlPaises do
        begin
          Close;
          SQL.Clear;
          SQL.Add('select * from PAISES');
          SQL.Add('where CODIGO_PAIS not like ''76''');
          SQL.Add('order by NOME_PAIS');
          Open;
        end;
    end;
 end;
  • I don’t understand your question: you cite "naturalness: city and state of birth", but want to have two Lookupcombobox to select the country? Or you wrote by mistake "naturalness"?

  • @Diego Felipe - Actually is nationality

  • @prmas - In fact it is nationality - First I have to know if it is Brazilian or Naturalized, if positive only Brazil as parents should be allowed, not being able to change. If foreign, all but Brazil may be allowed.

  • I don’t think I understand, try setting the Keyvalue property of the other Lookupcombobox to the desired value

1 answer

1

I have never worked with lookupcombobox but try to do it this way.

In the onexit event of the first combo do the following validation

if DBLookupComboBox1.listField = 'Brasileira' then
   begin
      DBLookupComboBox2.enabled := False;
      DBLookupComboBox2.listfieldIndex := Valor Do Pais;  // provavelmente deve ser o id do mesmo   
   end
else 
   begin
      DBLookupComboBox2.enabled := True;
      DBLookupComboBox2.listFieldIndex := 0;
   end;

I usually work with the same combobox, where I fill the same at runtime along with Firedac where ends up leaving the application even faster than that using with DB components.

However the idea is the same, locate the index for the record you want for a certain action performed within the system.

Browser other questions tagged

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