Sabe quando você tem varios Edits (ou textbox) num form, quer destacar com uma determinada cor
quando eles ganharem o foco mas todos eles já tem
eventos e você não quer sobrescrever, substituir e
nem mecher nos eventos deles? Tambem não quer escrever o
codigo pra mudar de cor no proprio evento, porque são varios
edits e você quer reaproveitamento de código e o
minimo possivel de retrabalho?
no C# isso é facil porque
você pode concatenar eventos, colocando facilmente dois
metodos para um mesmo evento. No javascript tambem dá para
você chamar dois ou mais metodos por evento. Mas e no C++,
delphi e Lazarus?
OBS.: Coloquei delphi e lazarus como
duas "linguagens" diferentes porque apesar de serem praticamente
iguais, no lazarus, para se trabalhar com variaveis de
métodos de objetos e atribuir metodos à variaveis
você precisa obrigatoriamente do operador de
endereço "@". No delphi tambem era assim antigamente, mas
não sei em que versão que isso mudou.
Com estas classes abaixo você
pode simplesmente criar um objeto, passando a form, a cor quando entra
e a cor quando "passa o mouse" como parametros que todos os Edits (ou
Textboxes) manterão os eventos originais.
A primeira classe é
simplesmente um container de eventos (esta é apenas um
conteiner de cores no
codigo em C#, uma vez que é desnecessario que ela armazene
os eventos), a outra classe
é uma lista desses containers.
A principio o codigo desta classe
trabalha somente com TEdit. Mas usando bem a RTTI,
ispecionando por nomes e valores de propriedades, usando os ancestrais
em comum, uma dose de if's e criatividade você pode adaptar
para que funcione com todos os componentes.
Num mesmo projeto eu apresento a classe
e a maneira de usar. Em breve postarei um componente para delphi feito
com esse conceito.
Como isso é só uma
demonstração, eu não estou
criando os metodos Acessores Get e Set para minhas propriedades. Estou
escrevendo direto no campo privado. Não faça isso
porque é muito feio, viola o encapsulamento e quem faz isso
não vai pro céu :) .
Primeiro C++ builder:
Cabeçalhos e interfaces das
classes (arquivo .h)
class TEventContainer: public TObject
{
private:
TNotifyEvent FOnEnter;
TNotifyEvent FOnExit;
TColor FCorOriginal;
__published:
__property TNotifyEvent OnEnter = {read=FOnEnter, write=FOnEnter};
__property TNotifyEvent OnExit = {read=FOnExit, write=FOnExit};
__property TColor CorOriginal = {read = FCorOriginal, write =
FCorOriginal};
};
class TMudaCor: public TObject
{
private:
TForm
*FForm;
TColor
FCorEntra;
TStringList *FlistaEvt;
public:
__fastcall virtual TMudaCor(TForm *frm, TColor corentra);
__fastcall virtual ~TMudaCor(void);
void
__fastcall DoEnter(TObject *sender);
void
__fastcall DoExit(TObject *sender);
};
class TForm1 : public TForm
{
__published: // IDE-managed Components
TEdit *Edit1;
TEdit *Edit2;
TEdit *Edit3;
TButton *Button1;
TButton *Button2;
TLabel *Label1;
void __fastcall Edit1Enter(TObject
*Sender);
void __fastcall Edit1Exit(TObject
*Sender);
void __fastcall Edit2Enter(TObject
*Sender);
void __fastcall Edit2Exit(TObject
*Sender);
void __fastcall FormCreate(TObject
*Sender);
void __fastcall FormDestroy(TObject
*Sender);
private: // User declarations
TMudaCor *mudacor;
public: //
User declarations
__fastcall TForm1(TComponent* Owner);
};
A implementação
(arquivo cpp)
//--------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//--------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//--------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner)
{
}
//--------------------------------------------------------------
void __fastcall TForm1::Edit1Enter(TObject *Sender)
{
Label1->Caption = "Entrou no edit
1";
}
//-------------------------------------------------------------
void __fastcall TForm1::Edit1Exit(TObject *Sender)
{
Label1->Caption = "Saiu do edit
1";
}
//-------------------------------------------------------------
void __fastcall TForm1::Edit2Enter(TObject *Sender)
{
Label1->Caption = "Entrou no edit
2";
}
//-------------------------------------------------------------
void __fastcall TForm1::Edit2Exit(TObject *Sender)
{
Label1->Caption = "saiu do edit
2";
}
//-------------------------------------------------------------
__fastcall TMudaCor::TMudaCor(TForm *frm, TColor corentra)
{
int i;
TEventContainer *evtc;
FlistaEvt = new TStringList();
FlistaEvt->Sorted = true;
TEdit *ed;
FForm = frm;
FCorEntra=corentra;
for (i=0; i<FForm->ComponentCount;i++)
{
if (
CompareStr(FForm->Components[i]->ClassName(),
"TEdit") == 0 )
{
evtc = new TEventContainer();
ed = (TEdit *)
FForm->Components[i];
evtc->OnEnter =
ed->OnEnter;
evtc->OnExit = ed->OnExit;
evtc->CorOriginal =
ed->Color;
FlistaEvt->AddObject(ed->Name, evtc);
ed->OnEnter= DoEnter;
ed->OnExit= DoExit;
};
};
FlistaEvt->Sort();
}
__fastcall TMudaCor::~TMudaCor(void)
{
int i;
for (i = 0 ; i <
FlistaEvt->Count ; i++)
if (FlistaEvt->Objects[i] != NULL)
FlistaEvt->Objects[i]->Free();
FlistaEvt->Free();
}
void __fastcall TMudaCor::DoEnter(TObject *Sender)
{
TEventContainer *evtc;
int idx;
if
(CompareStr(Sender->ClassName(), "TEdit") == 0)
{
((TEdit *)Sender)->Color =
FCorEntra;
}
idx =
FlistaEvt->IndexOf(((TEdit *)Sender)->Name);
if(idx > -1)
{
if (FlistaEvt->Objects[idx] != NULL)
{
evtc= (TEventContainer
*)(FlistaEvt->Objects[idx]);
}
if ((evtc != NULL ) && (
evtc->OnEnter != NULL)) {
evtc->OnEnter(Sender);
}
}
}
void __fastcall TMudaCor::DoExit(TObject *Sender)
{
TEventContainer *evtc;
int idx;
idx =
FlistaEvt->IndexOf(((TEdit *)Sender)->Name);
if(idx > -1)
{
if (FlistaEvt->Objects[idx] != NULL)
{
evtc= (TEventContainer
*)(FlistaEvt->Objects[idx]);
}
if (evtc != NULL )
{
if
(CompareStr(Sender->ClassName(), "TEdit") == 0)
{
((TEdit *)Sender)->Color =
evtc->CorOriginal;
}
if (
evtc->OnExit != NULL)
{
evtc->OnExit(Sender);
}
}
}
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
mudacor = new TMudaCor(this,
clYellow);
}
//------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
mudacor->Free();
}
//------------------------------------------------------------
O dfm:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 293
ClientWidth = 426
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
OnDestroy = FormDestroy
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 24
Top = 8
Width = 31
Height = 13
Caption = 'Label1'
end
object Edit1: TEdit
Left = 24
Top = 32
Width = 121
Height = 21
Color = clMoneyGreen
TabOrder = 0
Text = 'Edit1'
OnEnter = Edit1Enter
OnExit = Edit1Exit
end
object Edit2: TEdit
Left = 24
Top = 72
Width = 121
Height = 21
Color = clSkyBlue
TabOrder = 1
Text = 'Edit2'
OnEnter = Edit2Enter
OnExit = Edit2Exit
end
object Edit3: TEdit
Left = 24
Top = 120
Width = 121
Height = 21
Color = clSilver
TabOrder = 2
Text = 'Edit3'
end
object Button1: TButton
Left = 176
Top = 30
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 3
end
object Button2: TButton
Left = 176
Top = 70
Width = 75
Height = 25
Caption = 'Button2'
TabOrder = 4
end
end
em
C#:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace MudaCorCSharp
{
/// <summary>
/// Summary description for Form1.
/// </summary>
///
public class Form1 :
System.Windows.Forms.Form
{
private
System.Windows.Forms.TextBox textBox1;
private
System.Windows.Forms.TextBox textBox2;
private
System.Windows.Forms.TextBox textBox3;
private
System.Windows.Forms.Button button1;
private
System.Windows.Forms.Label label1;
private
System.Windows.Forms.TextBox a3;
private
System.Windows.Forms.TextBox a2;
private
System.Windows.Forms.TextBox a1;
///
<summary>
///
Required designer variable.
///
</summary>
private
System.ComponentModel.Container components = null;
public
Form1()
{
//
// Required for Windows Form Designer
support
//
InitializeComponent();
//
// TODO: Add any constructor code after
InitializeComponent call
//
}
///
<summary>
/// Clean
up any resources being used.
///
</summary>
protected
override void Dispose( bool disposing )
{
if( disposing )
{
if
(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region
Windows Form Designer generated code
///
<summary>
///
Required method for Designer support - do not modify
/// the
contents of this method with the code editor.
///
</summary>
private
void InitializeComponent()
{
this.textBox1 = new
System.Windows.Forms.TextBox();
this.textBox2 = new
System.Windows.Forms.TextBox();
this.textBox3 = new
System.Windows.Forms.TextBox();
this.button1 = new
System.Windows.Forms.Button();
this.label1 = new
System.Windows.Forms.Label();
this.a3 = new
System.Windows.Forms.TextBox();
this.a2 = new
System.Windows.Forms.TextBox();
this.a1 = new
System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.BackColor =
System.Drawing.Color.Gainsboro;
this.textBox1.Location = new
System.Drawing.Point(16, 72);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
this.textBox1.MouseEnter += new
System.EventHandler(this.textBox1_MouseEnter);
this.textBox1.Leave += new
System.EventHandler(this.textBox1_Leave);
this.textBox1.MouseLeave += new
System.EventHandler(this.textBox1_MouseLeave);
this.textBox1.Enter += new
System.EventHandler(this.textBox1_Enter);
//
// textBox2
//
this.textBox2.BackColor =
System.Drawing.Color.MistyRose;
this.textBox2.Location = new
System.Drawing.Point(16, 104);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 1;
this.textBox2.Text = "textBox2";
this.textBox2.MouseEnter += new
System.EventHandler(this.textBox2_MouseEnter);
this.textBox2.Leave += new
System.EventHandler(this.textBox2_Leave);
this.textBox2.MouseLeave += new
System.EventHandler(this.textBox2_MouseLeave);
this.textBox2.Enter += new
System.EventHandler(this.textBox2_Enter);
//
// textBox3
//
this.textBox3.BackColor =
System.Drawing.Color.PeachPuff;
this.textBox3.Location = new
System.Drawing.Point(16, 136);
this.textBox3.Name = "textBox3";
this.textBox3.TabIndex = 2;
this.textBox3.Text = "textBox3";
//
// button1
//
this.button1.Location = new
System.Drawing.Point(144, 24);
this.button1.Name = "button1";
this.button1.TabIndex = 3;
this.button1.Text = "button1";
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Location = new
System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.TabIndex = 4;
this.label1.Text = "label1";
//
// a3
//
this.a3.BackColor =
System.Drawing.Color.PaleTurquoise;
this.a3.Location = new
System.Drawing.Point(144, 136);
this.a3.Name = "a3";
this.a3.TabIndex = 7;
this.a3.Text = "textBox4";
//
// a2
//
this.a2.BackColor =
System.Drawing.Color.PaleGreen;
this.a2.Location = new
System.Drawing.Point(144, 104);
this.a2.Name = "a2";
this.a2.TabIndex = 6;
this.a2.Text = "textBox5";
//
// a1
//
this.a1.BackColor =
System.Drawing.Color.LightGoldenrodYellow;
this.a1.Location = new
System.Drawing.Point(144, 72);
this.a1.Name = "a1";
this.a1.TabIndex = 5;
this.a1.Text = "textBox6";
//
// Form1
//
this.AutoScaleBaseSize = new
System.Drawing.Size(5, 13);
this.ClientSize = new
System.Drawing.Size(292, 273);
this.Controls.Add(this.a3);
this.Controls.Add(this.a2);
this.Controls.Add(this.a1);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new
System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
///
<summary>
/// The
main entry point for the application.
///
</summary>
[STAThread]
static
void Main()
{
Application.Run(new Form1());
}
private
void textBox1_MouseEnter(object sender, System.EventArgs e)
{
label1.Text = "Mouse enter 1";
}
private
void textBox1_MouseLeave(object sender, System.EventArgs e)
{
label1.Text = "Mouse leave 1";
}
private
void textBox1_Enter(object sender, System.EventArgs e)
{
label1.Text = " enter 1";
}
private
void textBox1_Leave(object sender, System.EventArgs e)
{
label1.Text = "leave 1";
}
private
void textBox2_Enter(object sender, System.EventArgs e)
{
label1.Text = " enter 2";
}
private
void textBox2_Leave(object sender, System.EventArgs e)
{
label1.Text = "leave 2";
}
private
void textBox2_MouseEnter(object sender, System.EventArgs e)
{
label1.Text = "Mouse enter 2";
}
private
void textBox2_MouseLeave(object sender, System.EventArgs e)
{
label1.Text = "Mouse leave 2";
}
private
void button1_Click(object sender, System.EventArgs e)
{
}
private
void Form1_Load(object sender, System.EventArgs e)
{
TMudaCor mudacor = new TMudaCor(this,
System.Drawing.Color.Yellow, System.Drawing.Color.Orange);
}
}
public class TColorContainer
{
private
System.Drawing.Color FCorOriginal;
public
System.Drawing.Color CorOriginal {get{return FCorOriginal;}
set{FCorOriginal = value;}}
}
public class TMudaCor
{
private
System.Windows.Forms.Form FForm;
private
System.Drawing.Color FCorEntra;
private
System.Drawing.Color FCorMouseEntra;
//private
System.Collections.SortedList FlistaEvt;
private
System.Collections.Hashtable FlistaEvt;
public
TMudaCor(System.Windows.Forms.Form frm, System.Drawing.Color
corMouseEnter, System.Drawing.Color corEnter )
{
FForm = frm;
//FlistaEvt = new SortedList();
FlistaEvt = new Hashtable();
FCorEntra = corEnter;
FCorMouseEntra = corMouseEnter;
for(int i=0; i <
FForm.Controls.Count; i++)
{
if
(FForm.Controls[i] is TextBox)
{
TColorContainer evtc = new
TColorContainer();
evtc.CorOriginal = (FForm.Controls[i] as
TextBox).BackColor;
FlistaEvt.Add(FForm.Controls[i].Name,
evtc);
(FForm.Controls[i] as TextBox).Enter +=
new System.EventHandler( this.DoEnter);
(FForm.Controls[i] as TextBox).Leave +=
new System.EventHandler(this.DoExit);
(FForm.Controls[i] as
TextBox).MouseEnter += new System.EventHandler( this.DoMouseEnter);
(FForm.Controls[i] as
TextBox).MouseLeave += new System.EventHandler(this.DoMouseLeave);
}
}
}
public
void DoEnter(object sender, System.EventArgs e)
{
if(sender is TextBox)
{
(sender
as TextBox).BackColor = FCorEntra;
}
}
public
void DoExit(object sender, System.EventArgs e)
{
if(sender is TextBox)
{
TColorContainer evtc = (TColorContainer) FlistaEvt[(sender as
TextBox).Name];
if(evtc
!= null)
{
(sender as TextBox).BackColor =
evtc.CorOriginal;
}
}
}
public
void DoMouseEnter(object sender, System.EventArgs e)
{
if(sender is TextBox)
{
//sair
sem fazer nada se tiver o foco
if
((sender as TextBox).Focused) return;
(sender
as TextBox).BackColor = FCorMouseEntra;
}
}
public
void DoMouseLeave(object sender, System.EventArgs e)
{
if(sender is TextBox)
{
//sair
sem fazer nada se tiver o foco
if
((sender as TextBox).Focused) return;
TColorContainer evtc = (TColorContainer) FlistaEvt[(sender as
TextBox).Name];
if(evtc
!= null)
{
(sender as TextBox).BackColor =
evtc.CorOriginal;
}
}
}
}
}
Agora
a mesma coisa em delphi:
o Pas:
unit
Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics,
Controls, Forms,
Dialogs, StdCtrls;
type
TEventContainer = class
private
FOnEnter: TNotifyEvent;
FOnExit: TNotifyEvent;
FCorOriginal: TColor;
public
property OnEnter: TNotifyEvent read FOnEnter write FOnEnter;
property OnExit: TNotifyEvent read FOnExit write FOnExit;
property CorOriginal: TColor read FCorOriginal write FCorOriginal;
end;
TMudaCor = class
private
FForm: TForm;
FCorEntra: TColor;
FlistaEvt: TStringList;
public
constructor Create(frm: Tform; corentra:TColor); reintroduce;
destructor Destroy; override;
procedure DoEnter(sender: tobject);
procedure DoExit(sender: tobject);
end;
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
aEdit4: TEdit;
aEdit5: TEdit;
aEdit6: TEdit;
Label1: TLabel;
Label2: TLabel;
procedure aEdit6Enter(Sender: TObject);
procedure aEdit6Exit(Sender: TObject);
procedure Edit1Enter(Sender: TObject);
procedure Edit1Exit(Sender: TObject);
procedure Edit2Enter(Sender: TObject);
procedure Edit2Exit(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ private declarations }
mudador: TMudaCor;
public
{ public declarations }
end;
{ TMudaCor }
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.Edit1Enter(Sender: TObject);
begin
Label1.Caption:='entrou no
edit 1';
end;
procedure TForm1.aEdit6Enter(Sender: TObject);
begin
Label1.Caption:='entrou no
edit 6';
end;
procedure TForm1.aEdit6Exit(Sender: TObject);
begin
Label1.Caption:='saiu do edit
6';
end;
procedure TForm1.Edit1Exit(Sender: TObject);
begin
Label1.Caption:='saiu do edit 1';
end;
procedure TForm1.Edit2Enter(Sender: TObject);
begin
Label1.Caption:='entrou no edit 2';
end;
procedure TForm1.Edit2Exit(Sender: TObject);
begin
Label1.Caption:='saiu do edit 2';
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
mudador := TMudaCor.Create(self, clYellow);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
mudador.free;
end;
{ TMudaCor }
constructor TMudaCor.Create(frm: Tform; corentra:TColor);
var i: integer;
evtc: TEventContainer;
begin
inherited create;
FlistaEvt := TStringList.Create;
FlistaEvt.Sorted:=true;
FForm := frm;
FCorEntra:=corentra;
for i := 0 to FForm.ComponentCount-1 do
begin
if
(FForm.Components[i] is TEdit) then
begin
evtc := TEventContainer.Create;
evtc.OnEnter := (FForm.Components[i] as TEdit).OnEnter;
evtc.OnExit := (FForm.Components[i] as TEdit).OnExit;
evtc.CorOriginal := (FForm.Components[i] as TEdit).Color;
FlistaEvt.AddObject((FForm.Components[i] as TEdit).Name, evtc);
(FForm.Components[i] as TEdit).OnEnter:= DoEnter;
(FForm.Components[i] as TEdit).OnExit:= DoExit;
end;
end;
FlistaEvt.Sort;
end;
destructor TMudaCor.Destroy;
var i: integer;
begin
for i := 0 to FlistaEvt.Count
-1 do
if FlistaEvt.Objects[i] <> nil then
FlistaEvt.Objects[i].Free;
FlistaEvt.Free;
inherited Destroy;
end;
procedure TMudaCor.DoEnter(sender: tobject);
var evtc: TEventContainer;
idx: integer;
begin
if(sender is TEdit) then
(sender as TEdit).Color := FCorEntra;
idx :=
FlistaEvt.IndexOf((sender as TEdit).Name);
if(idx > -1) then
begin
if (FlistaEvt.Objects[idx] <> nil) then
begin
evtc:= (FlistaEvt.Objects[idx] as TEventContainer);
end;
if ((evtc <> nil) and ( Assigned(evtc.OnEnter))) then
evtc.OnEnter(sender);
end;
end;
procedure TMudaCor.DoExit(sender: tobject);
var evtc: TEventContainer;
idx: integer;
begin
idx :=
FlistaEvt.IndexOf((sender as TEdit).Name);
if(idx > -1) then
begin
if (FlistaEvt.Objects[idx] <> nil) then
begin
evtc:= (FlistaEvt.Objects[idx] as TEventContainer);
end;
if Assigned(evtc) then
begin
if (sender is TEdit) then
(sender as TEdit).Color := evtc.CorOriginal;
if Assigned(evtc.OnExit) then
evtc.OnExit(sender);
end;
end;
end;
end.
o DFM:
object Form1: TForm1
Left = 359
Top = 191
Width = 350
Height = 358
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = True
OnCreate = FormCreate
OnDestroy = FormDestroy
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 96
Top = 16
Width = 32
Height = 13
Caption = 'Label1'
Color = clBtnFace
ParentColor = False
end
object Label2: TLabel
Left = 8
Top = 184
Width = 249
Height = 65
Caption =
'Repare que o edit 3
n'#227'o tem evento no onexit'#13#10'onenter, mas mesm' +
'o assim a
mudan'#231'a de cor'#13#10'ocorre. Os outros edits, alem da mudan' +
#231'a de
'#13#10'cor ele continua fazendo o evento de antes,'#13#10'que era mud'
+
'ar o texto do
label1 para entrou, saiu etc'
Color = clBtnFace
ParentColor = False
end
object Edit1: TEdit
Left = 24
Top = 58
Width = 80
Height = 21
Color = clSkyBlue
TabOrder = 0
Text = 'Edit1'
OnEnter = Edit1Enter
OnExit = Edit1Exit
end
object Edit2: TEdit
Left = 24
Top = 98
Width = 80
Height = 21
Color = clMoneyGreen
TabOrder = 1
Text = 'Edit2'
OnEnter = Edit2Enter
OnExit = Edit2Exit
end
object Button1: TButton
Left = 224
Top = 56
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 2
end
object Button2: TButton
Left = 224
Top = 96
Width = 75
Height = 25
Caption = 'Button2'
TabOrder = 3
end
object Edit3: TEdit
Left = 24
Top = 136
Width = 80
Height = 21
Color = clMedGray
TabOrder = 4
Text = 'Edit3'
end
object aEdit4: TEdit
Left = 120
Top = 58
Width = 80
Height = 21
Color = clFuchsia
TabOrder = 5
Text = 'Edit1'
end
object aEdit5: TEdit
Left = 120
Top = 98
Width = 80
Height = 21
Color = clAqua
TabOrder = 6
Text = 'Edit2'
end
object aEdit6: TEdit
Left = 120
Top = 136
Width = 80
Height = 21
Color = clLime
TabOrder = 7
Text = 'Edit3'
OnEnter = aEdit6Enter
OnExit = aEdit6Exit
end
end
A
mesma coisa em Lazarus:
o
Pas
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls,
Graphics, Dialogs,
StdCtrls;
type
TEventContainer = class
private
FOnEnter: TNotifyEvent;
FOnExit: TNotifyEvent;
FCorOriginal: TColor;
public
property OnEnter: TNotifyEvent read FOnEnter write FOnEnter;
property OnExit: TNotifyEvent read FOnExit write FOnExit;
property CorOriginal: TColor read FCorOriginal write FCorOriginal;
end;
TMudaCor = class
private
FForm: TForm;
FCorEntra: TColor;
FlistaEvt: TStringList;
public
constructor Create(frm: Tform; corentra:TColor); reintroduce;
destructor Destroy; override;
procedure DoEnter(sender: tobject);
procedure DoExit(sender: tobject);
end;
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
aEdit4: TEdit;
aEdit5: TEdit;
aEdit6: TEdit;
Label1: TLabel;
Label2: TLabel;
procedure aEdit6Enter(Sender: TObject);
procedure aEdit6Exit(Sender: TObject);
procedure Edit1Enter(Sender: TObject);
procedure Edit1Exit(Sender: TObject);
procedure Edit2Enter(Sender: TObject);
procedure Edit2Exit(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ private declarations }
mudador: TMudaCor;
public
{ public declarations }
end;
{ TMudaCor }
var
Form1: TForm1;
implementation
{ TForm1 }
procedure TForm1.Edit1Enter(Sender: TObject);
begin
Label1.Caption:='entrou no
edit 1';
end;
procedure TForm1.aEdit6Enter(Sender: TObject);
begin
Label1.Caption:='entrou no
edit 6';
end;
procedure TForm1.aEdit6Exit(Sender: TObject);
begin
Label1.Caption:='saiu do edit
6';
end;
procedure TForm1.Edit1Exit(Sender: TObject);
begin
Label1.Caption:='saiu do edit 1';
end;
procedure TForm1.Edit2Enter(Sender: TObject);
begin
Label1.Caption:='entrou no edit 2';
end;
procedure TForm1.Edit2Exit(Sender: TObject);
begin
Label1.Caption:='saiu do edit 2';
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
mudador := TMudaCor.Create(self, clYellow);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
mudador.free;
end;
{ TMudaCor }
constructor TMudaCor.Create(frm: Tform; corentra: TColor);
var i: integer;
evtc: TEventContainer;
begin
inherited create;
FlistaEvt := TStringList.Create;
FlistaEvt.Sorted:=true;
FForm := frm;
FCorEntra:=corentra;
for i := 0 to FForm.ComponentCount-1 do
begin
if
(FForm.Components[i] is TEdit) then
begin
evtc := TEventContainer.Create;
evtc.OnEnter := (FForm.Components[i] as TEdit).OnEnter;
evtc.OnExit := (FForm.Components[i] as TEdit).OnExit;
evtc.CorOriginal:= (FForm.Components[i] as TEdit).Color;
FlistaEvt.AddObject((FForm.Components[i] as TEdit).Name, evtc);
(FForm.Components[i] as TEdit).OnEnter:=@DoEnter;
(FForm.Components[i] as TEdit).OnExit:=@DoExit;
end;
end;
FlistaEvt.Sort;
end;
destructor TMudaCor.Destroy;
var i: integer;
begin
for i := 0 to FlistaEvt.Count
-1 do
if FlistaEvt.Objects[i] <> nil then
FlistaEvt.Objects[i].Free;
FlistaEvt.Free;
inherited Destroy;
end;
procedure TMudaCor.DoEnter(sender: tobject);
var evtc: TEventContainer;
idx: integer;
begin
if(sender is TEdit) then
(sender as TEdit).Color := FCorEntra;
idx :=
FlistaEvt.IndexOf((sender as TEdit).Name);
if(idx > -1) then
begin
if (FlistaEvt.Objects[idx] <> nil) then
begin
evtc:= (FlistaEvt.Objects[idx] as TEventContainer);
end;
if ((evtc <> nil) and (evtc.OnEnter <>
nil)) then
evtc.OnEnter(sender);
end;
end;
procedure TMudaCor.DoExit(sender: tobject);
var evtc: TEventContainer;
idx: integer;
begin
idx :=
FlistaEvt.IndexOf((sender as TEdit).Name);
if(idx > -1) then
begin
if (FlistaEvt.Objects[idx] <> nil) then
begin
evtc:= (FlistaEvt.Objects[idx] as TEventContainer);
end;
if (evtc <> nil) then
begin
if (sender is TEdit) then
(sender as TEdit).Color := evtc.CorOriginal;
if (evtc.OnExit <> nil) then
evtc.OnExit(sender);
end;
end;
end;
initialization
{$I unit1.lrs}
end.
o LFM
object
Form1: TForm1
Left = 359
Height = 331
Top = 191
Width = 342
Caption = 'Form1'
ClientHeight = 331
ClientWidth = 342
OnCreate = FormCreate
OnDestroy = FormDestroy
ParentFont = False
LCLVersion = '0.9.26'
object Label1: TLabel
Left = 96
Height = 14
Top = 16
Width = 32
Caption = 'Label1'
ParentColor = False
end
object Label2: TLabel
Left = 8
Height = 66
Top = 184
Width = 259
Caption = 'Repare que o edit 3
não tem evento no onexit'#13#10'onenter, mas mesmo
assim a mudança de cor'#13#10'ocorre. Os outros edits, alem
da mudança de '#13#10'cor ele continua fazendo o evento de
antes,'#13#10'que era mudar o texto do label1 para entrou, saiu etc'
ParentColor = False
end
object Edit1: TEdit
Left = 24
Height = 23
Top = 58
Width = 80
Color = clSilver
OnEnter = Edit1Enter
OnExit = Edit1Exit
TabOrder = 0
Text = 'Edit1'
end
object Edit2: TEdit
Left = 24
Height = 23
Top = 98
Width = 80
Color = clSkyBlue
OnEnter = Edit2Enter
OnExit = Edit2Exit
TabOrder = 1
Text = 'Edit2'
end
object Button1: TButton
Left = 224
Height = 25
Top = 56
Width = 75
Caption = 'Button1'
TabOrder = 2
end
object Button2: TButton
Left = 224
Height = 25
Top = 96
Width = 75
Caption = 'Button2'
TabOrder = 3
end
object Edit3: TEdit
Left = 24
Height = 23
Top = 136
Width = 80
Color = clMoneyGreen
TabOrder = 4
Text = 'Edit3'
end
object aEdit4: TEdit
Left = 120
Height = 23
Top = 58
Width = 80
Color = clFuchsia
TabOrder = 5
Text = 'Edit1'
end
object aEdit5: TEdit
Left = 120
Height = 23
Top = 98
Width = 80
Color = clAqua
TabOrder = 6
Text = 'Edit2'
end
object aEdit6: TEdit
Left = 120
Height = 23
Top = 136
Width = 80
Color = clLime
OnEnter = aEdit6Enter
OnExit = aEdit6Exit
TabOrder = 7
Text = 'Edit3'
end
end
Espero que esse artigo seja util para quem precisar implementar algo
assim sem mecher no codigo já existente, ou para quem deseja
estudar as semelhanças e diferenças entre as varias
linguagens de programação.
baixe o conjunto de exemplos no meu sky drive:
clicando aqui
Comentários
Postar um comentário