Ajuda com componente TChackBox no Delphi

Delphi

17/08/2016

Bom dia, tenho 6 componentes TCheckBox em um form e gostaria de desabilitar 5 deles após escolher um dos 6, mais ao desmarcar esse que foi escolhido voltar a habilitar os 5.
Emanuel Gonçalves

Emanuel Gonçalves

Curtidas 0

Respostas

Emanuel Gonçalves

Emanuel Gonçalves

17/08/2016

tive uma ideia, o problema agora está no ( FDLerCodLanc.SQL.Add('OR '); ) . me retorna o erro ( Token Unknown - line 3, columm1 OR )

procedure TfrmRelCodLanc.Monta_SQL();
var i : Integer;
    Entra : Boolean;
begin
    FDLerCodLanc.Active := False;
    FDLerCodLanc.SQL.Clear;
    FDLerCodLanc.SQL.Add(' SELECT * FROM CODLANC ');
    Entra := True;

    for i := 0 to Self.ComponentCount - 1 do
    begin
        if (Self.Components[i] is TCheckBox) and ( TCheckBox(Self.Components[i]).Checked) then
        begin
            If Entra then
            begin
                FDLerCodLanc.SQL.Add('WHERE ');
                Entra := False;
            end
            else
                FDLerCodLanc.SQL.Add('OR ');             <<<<<<<<<<<<<<<<<<< O detalhe está aqui, me retorna o erro ( Token Unknown - line 3, columm1 OR  )
                Case TCheckBox(Self.Components[i]).Tag of
                    1:  FDLerCodLanc.SQL.Add('L_TIPO = ''D'' ');
                    2:  FDLerCodLanc.SQL.Add('L_TIPO = ''R'' ');
                    3:  FDLerCodLanc.SQL.Add('L_TIPO = ''E'' ');
                    4:  FDLerCodLanc.SQL.Add('L_TIPO = ''S'' ');
                    5:  FDLerCodLanc.SQL.Add('L_TIPO = ''M'' ');
                    6:  FDLerCodLanc.SQL.Add('L_TIPO = ''U'' ');
                end;
        end;
    end;

    if RdGpListaPor.ItemIndex = 0 then
       FDLerCodLanc.SQL.Add('ORDER BY L_TIPO, L_CODIGO')
    else
        FDLerCodLanc.SQL.Add('ORDER BY L_TIPO, L_DESCRICAO');

    FDLerCodLanc.Active := True;
    FDLerCodLanc.First;
end;

GOSTEI 0
Natanael Ferreira

Natanael Ferreira

17/08/2016

Sugiro trocar os checkbox's pelo componente RadioGroup onde você pode escolher apenas uma opção dentre as disponíveis.

Ou CheckListBox que nativamente você pode escolher mais de uma opção, mas com o código abaixo no evento OnClickChecked fica exclusivo igual o RadiogGroup.

var
  i: Integer;
begin
  with TCheckListBox(Sender) do
    if (Checked[ItemIndex]) then
    begin
      Items.BeginUpdate;

      for i := 0 to Pred(Items.Count) do
        if (i <> ItemIndex) then
          Checked[i] := False;

      Items.EndUpdate;
    end;
end;
GOSTEI 0
Emanuel Gonçalves

Emanuel Gonçalves

17/08/2016

Obrigado Natanael,

Mas fiquei curioso para resolver o erro, não consigo entender o pq desse erro.
GOSTEI 0
Natanael Ferreira

Natanael Ferreira

17/08/2016

Provavelmente é algum detalhe na montagem do SQL que está incorreto.

Coloque um ShowMessage buscando o texto do SQL antes de ativar a query para ver como ele está sendo montado.

procedure TfrmRelCodLanc.Monta_SQL();
var
  i: Integer;
  Entra: Boolean;
begin
  FDLerCodLanc.Active := False;
  FDLerCodLanc.SQL.Clear;
  FDLerCodLanc.SQL.Add(' SELECT * FROM CODLANC ');
  Entra := True;

  for i := 0 to Self.ComponentCount - 1 do
  begin
    if (Self.Components[i] is TCheckBox) and (TCheckBox(Self.Components[i]).Checked) then
    begin
      If Entra then
      begin
        FDLerCodLanc.SQL.Add('WHERE ');
        Entra := False;
      end
      else
        FDLerCodLanc.SQL.Add('OR ');

      Case TCheckBox(Self.Components[i]).Tag of
        1:
          FDLerCodLanc.SQL.Add('L_TIPO = ''D'' ');
        2:
          FDLerCodLanc.SQL.Add('L_TIPO = ''R'' ');
        3:
          FDLerCodLanc.SQL.Add('L_TIPO = ''E'' ');
        4:
          FDLerCodLanc.SQL.Add('L_TIPO = ''S'' ');
        5:
          FDLerCodLanc.SQL.Add('L_TIPO = ''M'' ');
        6:
          FDLerCodLanc.SQL.Add('L_TIPO = ''U'' ');
      end;
    end;
  end;

  if RdGpListaPor.ItemIndex = 0 then
    FDLerCodLanc.SQL.Add('ORDER BY L_TIPO, L_CODIGO')
  else
    FDLerCodLanc.SQL.Add('ORDER BY L_TIPO, L_DESCRICAO');

  ShowMessage(FDLerCodLanc.SQL.Text); // Acrescentei esta linha

  FDLerCodLanc.Active := True;
  FDLerCodLanc.First;
end;
GOSTEI 0
Emanuel Gonçalves

Emanuel Gonçalves

17/08/2016

Ele ta criando o seguinte SQL

select * from CODLANC
where
or <<<<<<<<<<<<< aqui está o problema e não consigo resolver
L_TIPO = 'D'
or
L_TIPO = 'S'
order by L_TIPO, L_CODIGO
GOSTEI 0
Natanael Ferreira

Natanael Ferreira

17/08/2016

Fiz uma simulação do seu cenário aqui e seu código está correto.

Para que funcione, os 6 checkbox's devem estar com a propriedade Tag numerada de 1 a 6.

Se não estiver, dará este problema.
GOSTEI 0
Emanuel Gonçalves

Emanuel Gonçalves

17/08/2016

eu identifiquei o problema , o form tinha um sétimo componente Tcheckbox com a tag 0, a solução foi

if (Self.Components[i] is TCheckBox) and ( TCheckBox(Self.Components[i]).Checked) and (TCheckBox(Self.Components[i]).Tag > 0)
GOSTEI 0
Emanuel Gonçalves

Emanuel Gonçalves

17/08/2016

obrigado
GOSTEI 0
POSTAR