Ao Clicar no Botão como abrir uma janela de pergunta

Delphi

31/03/2014

Olá Pessoal,

O que eu quero fazer é que ao clicar no botão imprimir apareça uma pergunta se ele "Deseja Imprimir todos ou Imprimir somente aquele Fornecedor?" onde tenha 2 botões para ele escolher qual das opções a pessoa deseja.

Eu entendo que eu posso fazer isso criando uma nova Unit e chamar como um showmodal. Mas não teria alguma forma mais correta ou mais pratica de fazer para eu não ter que criar uma Unit somente para essa pergunta?

A janela tem que ser showmodal pois ela vai ter que escolher uma opção para poder seguir adiante.

Desde já agradecido!
Edson Vilhalba

Edson Vilhalba

Curtidas 0

Respostas

Thiago Irrazabal

Thiago Irrazabal

31/03/2014

Boa noite, criei um projeto aqui de testa, veja se te serve.

Único componente que coloquei na tela do Formulário foi um TButton, o nome dele ficou como btnImprimir, o formulário ficou com o nome fDlg e a Unit eu salvei com nome de uDlg.

Segue abaixo o código do projeto.
unit uDlg;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TFlags = (mrAllForn = 0, mrOneForn = 1);

  TfDlg = class(TForm)
    btnImprimir: TButton;
    procedure btnImprimirClick(Sender: TObject);
  private
    { Private declarations }
    function ShowDlg(Texto: String; Flags: array of TFlags): TFlags;
  public
    { Public declarations }
  end;

var
  fDlg: TfDlg;

implementation

{$R *.dfm}

procedure TfDlg.btnImprimirClick(Sender: TObject);
begin
  if ShowDlg('Deseja Imprimir todos ou Imprimir somente esse Fornecedor?', [mrAllForn, mrOneForn]) = mrAllForn then
    ShowMessage('A')
  else
    ShowMessage('B');
end;

function TfDlg.ShowDlg(Texto: String; Flags: array of TFlags): TFlags;
var
  NewForm: TForm;
  NewLabel: TLabel;
  NewButton: TButton;
  Count, I: Integer;
begin
  try
    NewForm := TForm.Create(nil);
    NewForm.Position := poDesktopCenter;
    
    if (Length(Texto) * 6) > 280 then
      NewForm.Width := Length(Texto) * 6
    else
      NewForm.Width := 300;

    NewForm.Height := 110;
    NewForm.BorderStyle := bsDialog;

    NewLabel := TLabel.Create(NewForm);
    NewLabel.Left := 25;
    NewLabel.Top := 10;
    NewLabel.Width := Length(Texto);
    NewLabel.Height := 25;
    NewLabel.Caption := Texto;
    NewLabel.Parent := NewForm;
    NewLabel.Visible := True;

    Count := (NewForm.Width div 2) - 130;
    for I := 0 to Length(Flags) -1 do
      begin
        NewButton := TButton.Create(NewForm);
        NewButton.Left := Count - 2;
        NewButton.Top := 45;
        NewButton.Width := 130;

        case Flags[I] of
          mrAllForn:
            begin
              NewButton.Caption := 'Todos Fornecedores';
              NewButton.ModalResult := 1;
            end;

          mrOneForn:
            begin
              NewButton.Caption := 'Fornecedor Selecionado';
              NewButton.ModalResult := 2;
            end;
        end;

        NewButton.Parent := NewForm;
        NewButton.Visible := True;
        Count := Count + NewButton.Width + 6;
      end;

    case NewForm.ShowModal of
      1: Result := mrAllForn;
      2: Result := mrOneForn;
    end;

  finally
    FreeAndNil(NewForm);
  end;
end;

end.




Att,
Thiago Irrazabal de Oliveira.
GOSTEI 0
Edson Vilhalba

Edson Vilhalba

31/03/2014

Obrigado Thiago Irrazabal,

Irei testar!
GOSTEI 0
POSTAR