criar evento onchange via código neste...

30/11/2005

0


unit Funcoes;

interface

Uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, ExtCtrls, DBTables, DBGrids, Grids, variants,
  DBClient, SqlExpr, DBXpress, DB, ComCtrls, DateUtils, Consts;

procedure Search(DS : TDataSet; cField: String);

implementation

Procedure Search(DS : TDataSet; cField: String);
var
  Form: TForm;
  Panel : TPanel;
  Prompt: TLabel;
  Edit: TEdit;

begin
  Form := TForm.Create(Application);
  with Form do
    try
      Canvas.Font := Font;
      BorderStyle := bsDialog;
      Width  := 390;
      Height := 160;
      Caption := ´Pesquisar por: ´+DS.fieldbyname(cField).DisplayLabel;
      Position := poScreenCenter;
      Prompt := TLabel.Create(Form);
      with Prompt do
      begin
        Parent := Form;
        Caption := ´Informe o termo a ser localizado:´;
        Left := 10;
        Top := 10;
        WordWrap := True;
      end;
      Edit := TEdit.Create(Form);
      with Edit do
      begin
        Parent := Form;
        Left := Prompt.Left;
        Top := Prompt.Top + Prompt.Height + 5;
        Width := 359;
        MaxLength := 255;
        CharCase := ecUpperCase;
        Text := ´´;
        SelectAll;
      end;
      Panel := TPanel.Create(Form);
      with Panel do
      begin
        Parent := Form;
        Align := AlBottom;
        BevelOuter := bvNone;
        Height := 32;
      end;
      with TButton.Create(Form) do
      begin
        Parent := Panel;
        Caption := ´&Ok´;
        ModalResult := mrYes;
        Default := True;
        Width  := 75;
        Height := 25;
        Left := 210;
      end;
      with TButton.Create(Form) do
      begin
        Parent := Panel;
        Caption := ´&Cancelar´;
        ModalResult := mrCancel;
        Cancel := True;
        Width  := 75;
        Height := 25;
        Left :=  290;
      end;
      ShowModal;
//  DS.Locate(DS.fieldbyname(cField).DisplayName,EDit.text,[loPartialkey]);
    finally
      Form.Free;
    end;
end;


Este código acima utilizo como pesquisa em todos os meus forms, gostaria de criar um evento onchange para o edit pois quando o usuário digitar algo fosse sendo executada a seguinte linha:

DS.Locate(DS.fieldbyname(cField).DisplayName,EDit.text,[loPartialkey]);

Mas tá dificil, alguem pode dar uma dica, algo que possa estar fazendo errado, outra forma de se criar o form, os botões, o edit e o evento onchange tudo via código.

Obrigado

César


Cesarpir

Cesarpir

Responder

Posts

30/11/2005

Jairroberto

Olá, César!

O evento ´OnChange´ é do tipo ´TNotifyEvent´ que, por sua vez, é um ´procedure of object´, ou seja, é um procedimento [b:89fd1a5a26]de um objeto[/b:89fd1a5a26]. Repare que ´de um objeto´ está destacado propositadamente, pois indica que você precisa atribuir o evento ´OnChange´ a um procedimento ´de um objeto´ qualquer, mas tem que ser ´de um objeto´.

A solução me parece simples: você não precisa criar tudo diretamente via código em um procedimento perdido em um Unit cheia de funções utilitárias. Crie o formulário, com o ´Prompt´, com o ´Edit´ (já com o procedimento ´OnChange´ definido), com o ´Panel´ e com os botões, alterando a função ´Search´ que você criou, da seguinte forma:

Procedure Search(DS : TDataSet; cField: String);
var
  FormSearch: TFormSearch;
begin
  FormSearch := TFormSearch.Create(Application);
  try
    FormSearch.Caption := ´Pesquisar por: ´+DS.fieldbyname(cField).DisplayLabel;
    FormSearch.ShowModal;
  finally
    FormSearch.Release;
  end;
end;



Mas se você insiste em fazer isso via código, vai precisar criar um objeto para definir o ´OnChange´ do ´Edit´. Ficaria mais ou menos assim:

unit Funcoes;

interface

Uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, ExtCtrls, DBTables, DBGrids, Grids, variants,
  DBClient, SqlExpr, DBXpress, DB, ComCtrls, DateUtils, Consts;

type
  TSearchObject = class(TObject)
  private
    FDS: TDataSet;
    FFieldName: string;
    procedure EditOnChange(Sender: TObject);
    procedure SetFieldName(const Value: string);
  public
    Form: TForm;
    Panel : TPanel;
    Prompt: TLabel;
    Edit: TEdit;
    constructor Create;
    class function Executar(DS : TDataSet; cField: String);
    property DS: TDataSet read FDS write FDS;
    property FieldName: string read FFieldName write SetFieldName;
  end;

implementation

{ TSearchObject }
constructor TSearchObject.Create;
begin
    Form := TForm.Create(Application);
    with Form do
    begin
      Canvas.Font := Font;
      BorderStyle := bsDialog;
      Width  := 390;
      Height := 160;
      Position := poScreenCenter;
      Prompt := TLabel.Create(Form);
      with Prompt do
      begin
        Parent := Form;
        Caption := ´Informe o termo a ser localizado:´;
        Left := 10;
        Top := 10;
        WordWrap := True;
      end;
      Edit := TEdit.Create(Form);
      with Edit do
      begin
        Parent := Form;
        Left := Prompt.Left;
        Top := Prompt.Top + Prompt.Height + 5;
        Width := 359;
        MaxLength := 255;
        CharCase := ecUpperCase;
        Text := ´´;
        SelectAll;
        OnChange := EditOnChange;
      end;
      Panel := TPanel.Create(Form);
      with Panel do
      begin
        Parent := Form;
        Align := AlBottom;
        BevelOuter := bvNone;
        Height := 32;
      end;
      with TButton.Create(Form) do
      begin
        Parent := Panel;
        Caption := ´&Ok´;
        ModalResult := mrYes;
        Default := True;
        Width  := 75;
        Height := 25;
        Left := 210;
      end;
      with TButton.Create(Form) do
      begin
        Parent := Panel;
        Caption := ´&Cancelar´;
        ModalResult := mrCancel;
        Cancel := True;
        Width  := 75;
        Height := 25;
        Left :=  290;
      end;
    end;
end;

class function TSearchObject.Executar(DS: TDataSet; cField: String): Boolean;
var
  SearchObject: TSearchObject;
begin
  SearchObject := TSearchObject.Create;
  try
    SearchObject.DS := DS;
    SearchObject.FieldName := cField;
    Result := IsPositiveResult(SearchObject.Form.ShowModal);
  finally
    SearchObject.Free;
  end;
end;

procedure TSearchObject.EditOnChange(Sender: TObject);
begin
  DS.Locate(DS.FieldByName(FieldName).DisplayName, Edit.Text,[loPartialkey]);
end;

procedure TSearchObject.SetFieldName(const Value: string);
begin
  FFieldName := Value;
  Form.Caption := ´Pesquisar por: ´+DS.FieldByName(Value).DisplayLabel;
end;


Não testei!

Um abraço,
Jair


Responder

30/11/2005

Cesarpir

Obrigado Jair vou testar agora e ver o que dá, valeu

Abraços

César


Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar