Olá
galera, nesta Quick Tips, irei mostra como podemos habilitar ou desabilitar
componentes dentro de um Formulário de maneira bem simples, vejamos como
implementar, para este exemplo vamos precisar de dois Forms, FrmPrincipal, FrmComponentes :
No FrmPrincipal vamos adicionar :
2
TButton(BtnAtivar, BtnDesativar)
No FrmComponentes vamos adicionar :
1 TEdit
1 TCombobox
1 TMemo
1 TMaskEdit
1 TLabeledEdit
unit uFrmPrincipal;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Mask, Buttons;
type
TFrmPrincipal = class(TForm)
Panel1: TPanel;
BtnAtivar: TBitBtn;
BtnDesativar: TBitBtn;
procedure BtnDesativarClick(Sender: TObject);
procedure BtnAtivarClick(Sender: TObject);
private
procedure HabFieldsForm(xForm: TForm; Hab: Boolean);
{ Private declarations }
public
{ Public declarations }
end;
var
FrmPrincipal: TFrmPrincipal;
implementation
uses uFrmComponentes;
{$R *.dfm}
{ Vejamos nossa função onde ela tem dois parametros o Form e se vamos ou não habilitar os controles deste Form }
procedure TFrmPrincipal.HabFieldsForm(xForm : TForm; Hab: Boolean);
var
x : integer;
nome: String;
begin
for x := 0 to xForm.ControlCount - 1 do
if (xForm.Controls[x] is TEdit) or (xForm.Controls[x] is TComboBox) or
(xForm.Controls[x] is TMemo) or (xForm.Controls[x] is TMaskEdit) or
(xForm.Controls[x] is TLabeledEdit) then
begin
xForm.Controls[x].Enabled := Hab;
nome := xForm.Controls[x].Name;
with TEdit(xForm.FindComponent(nome)) do
if Hab = True then
Color := clYellow
else
Color := clBlack;
end;
end;
procedure TFrmPrincipal.BtnAtivarClick(Sender: TObject);
begin
if Not Assigned(FrmComponentes) then
FrmComponentes := TFrmComponentes.Create(Self);
HabFieldsForm(FrmComponentes,True);
BtnAtivar.Enabled := BtnDesativar.Enabled;
BtnDesativar.Enabled := Not BtnAtivar.Enabled;
FrmComponentes.ShowModal;
end;
procedure TFrmPrincipal.BtnDesativarClick(Sender: TObject);
begin
if Not Assigned(FrmComponentes) then
FrmComponentes := TFrmComponentes.Create(Self);
HabFieldsForm(FrmComponentes,False);
BtnAtivar.Enabled := BtnDesativar.Enabled;
BtnDesativar.Enabled := Not BtnAtivar.Enabled;
FrmComponentes.ShowModal;
end;
end.
Fico
por aqui ate à próxima Quick Tips.
Um
abraço
Wesley Y