unit UEstado;

interface

uses
  Windows, Messages, Classes, SysUtils, Graphics, Controls, StdCtrls, Forms,
  Dialogs, DBCtrls, DB, DBTables, Mask, ExtCtrls, Grids, DBGrids, Buttons,
  ToolWin, ComCtrls, ActnList;

type

THackDBGrid = class(TDBGrid);

  TFestado = class(TForm)
    Panel1: TPanel;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    Label2: TLabel;
    EditCOD_UF: TDBEdit;
    EditESTADO: TDBEdit;
    ToolBar2: TToolBar;
    btnPrimeiro: TSpeedButton;
    btnVoltar: TSpeedButton;
    btnAvancar: TSpeedButton;
    btnUltimo: TSpeedButton;
    Panel7: TPanel;
    btnnovo: TSpeedButton;
    btngravar: TSpeedButton;
    btncancelar: TSpeedButton;
    Label3: TLabel;
    DBEdit1: TDBEdit;
    Label4: TLabel;
    Label5: TLabel;
    ActionList1: TActionList;
    novo: TAction;
    avancar: TAction;
    voltar: TAction;
    StatusBar1: TStatusBar;
    SpeedButton21: TSpeedButton;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    Panel5: TPanel;
    DBGrid1: TDBGrid;
    ajuda: TAction;
    Label6: TLabel;
    DBEdit2: TDBEdit;
    procedure btnPrimeiroClick(Sender: TObject);
    procedure btnVoltarClick(Sender: TObject);
    procedure btnAvancarClick(Sender: TObject);
    procedure btnUltimoClick(Sender: TObject);
    procedure btnnovoClick(Sender: TObject);
    procedure btngravarClick(Sender: TObject);
    procedure btncancelarClick(Sender: TObject);
    procedure Label1Click(Sender: TObject);
    procedure DBGrid1Enter(Sender: TObject);
    procedure DBGrid1Exit(Sender: TObject);
    procedure Label2Click(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure novoExecute(Sender: TObject);
    procedure avancarExecute(Sender: TObject);
    procedure voltarExecute(Sender: TObject);
    procedure EditCOD_UFKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure EditESTADOKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormActivate(Sender: TObject);
    procedure SpeedButton21Click(Sender: TObject);
    procedure EditCOD_UFExit(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ajudaExecute(Sender: TObject);
  private
      procedure AppHint(Sender : TObject);
    { private declarations }
  public
  BOK : boolean;
  ST : String;
    { public declarations }
  end;

var
  Festado: TFestado;

implementation

uses udm;

procedure Tfestado.AppHint(Sender: TObject);
begin
  StatusBar1.Panels[0].Text := Application.Hint;
end;

{$R *.DFM}

procedure TFestado.btnPrimeiroClick(Sender: TObject);
begin
         fdm.tbestado.first;


end;

procedure TFestado.btnVoltarClick(Sender: TObject);
begin
        fdm.tbestado.prior;


end;

procedure TFestado.btnAvancarClick(Sender: TObject);
begin
        fdm.tbestado.Next;

end;

procedure TFestado.btnUltimoClick(Sender: TObject);
begin
        fdm.tbestado.last;
        

end;

procedure TFestado.btnnovoClick(Sender: TObject);
begin
        novoexecute(self);
end;

procedure TFestado.btngravarClick(Sender: TObject);
begin
        fdm.tbestado.edit;
         fdm.tbestado.post;
          btnGravar.Enabled:=false;
           btncancelar.enabled := false;
end;

procedure TFestado.btncancelarClick(Sender: TObject);
begin
        fdm.tbestado.cancel;
          btnGravar.Enabled:=false;
           btncancelar.enabled := false;
end;

procedure TFestado.Label1Click(Sender: TObject);
begin
        BOK := inputquery('Localizar UF','Entre com a sigla:',ST);
         if BOK then
          if not fdm.tbestado.Locate ('Estado',Uppercase(ST),[]) then
           ShowMessage ('UF não cadastrado !!!');
            ST := '';
end;

procedure TFestado.DBGrid1Enter(Sender: TObject);
begin
        keypreview := false;
end;

procedure TFestado.DBGrid1Exit(Sender: TObject);
begin
        keypreview := true;
end;

procedure TFestado.Label2Click(Sender: TObject);
begin
        BOK := inputquery('Localizar Estado','Entre com o nome de estado:',ST);
         if BOK then
          if not fdm.tbestado.Locate ('Nome',Uppercase(ST),[]) then
           ShowMessage ('Estado não cadastrado !!!');
            ST := '';
end;

procedure TFestado.FormKeyPress(Sender: TObject; var Key: Char);
begin
 if key=#13 then begin
  Key := #0;
       Perform(WM_nextdlgctl,0,0)
 end else if Key = #27 then
   Perform(WM_nextdlgctl,1,0);
end;

procedure TFestado.novoExecute(Sender: TObject);
begin
       fdm.tbestado.insert;
      EditCOD_UF.setfocus;
     btnGravar.Enabled:=true;
    btncancelar.Enabled := true;     
end;

procedure TFestado.avancarExecute(Sender: TObject);
begin
        btnavancarclick(self);
end;

procedure TFestado.voltarExecute(Sender: TObject);
begin
        btnvoltarclick(self);
end;

procedure TFestado.EditCOD_UFKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
        if key = VK_F3 then
         Label1Click(Self);
end;

procedure TFestado.EditESTADOKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
        if key = VK_F3 then
         Label2Click(Self);
end;

procedure TFestado.FormCloseQuery(Sender: TObject; var CanClose: Boolean);

begin
        IF btngravar.Enabled = true then begin
         case application.MessageBox('Deseja salvar as alterações?', 'Sair do Cadastro de Vendedores',mb_yesnocancel + MB_ICONQUESTION )of

         idyes : begin
         fdm.tbestado.edit;
          fdm.tbestado.post;
           canclose:=true;
            end;

         idno : begin
          fdm.tbestado.cancel;
           canclose:=true;
            end;

         idcancel :
           canclose:=false;
end;
end;
end;
procedure TFestado.FormActivate(Sender: TObject);
begin
        THackDBGrid(DBGrid1).FixedCols := 5;
end;

procedure TFestado.SpeedButton21Click(Sender: TObject);
begin
          if Application.MessageBox('Deseja excluir este estado? ',
         'Confirme', 4 + MB_ICONQUESTION)= idYes then begin

        fdm.tbestado.Delete;
        btngravar.Enabled := false;
        btncancelar.Enabled := false;
        end;
end;

procedure TFestado.EditCOD_UFExit(Sender: TObject);
begin
        btngravarclick(self);
end;

procedure TFestado.FormCreate(Sender: TObject);
begin
  Application.OnHint := AppHint;
end;

procedure TFestado.FormClose(Sender: TObject; var Action: TCloseAction);
begin
        Application.OnHint := nil;  
end;

procedure TFestado.ajudaExecute(Sender: TObject);
var
  Comp: Trichedit; // * variável Comp da classe TMemo
  texto : string;
begin
  Comp := Trichedit.Create (Self);  // * cria o componente Memo
  Comp.Parent := Self;    // * exibe o componente criado

  if fileexists (extractfilepath(paramstr(0)) + '\ajuda\ajudaestado.txt') then
   comp.lines.loadfromfile (extractfilepath(paramstr(0)) + '\ajuda\ajudaestado.txt');

  texto := comp.Text;
  comp.Destroy;

  Showmessage (texto);


end;

end.
