Title: How to relate a control to other controls in a Form
Question: I was looking for a good mode to relate my component to a list of others controls in the same form, but what I found was complicated to manage. So I found that home-made solution.
Answer:
This is a example unit of a TCompontent that enables and disables a bounce of other controls in the same window which name is included in the list of components to use.
Just compile, drop on a form, insert the names in the TStringList and then use:
EnablerDisabler1.EnableAll;
or
EnablerDisabler1.DisableAll;
---------------------------------------------------------------
Unit uEnabDisab;
Interface
Uses
Windows, Messages, SysUtils, Classes,
Graphics, Controls, Forms, Dialogs;
Type
TEnablerDisabler =
Class( TComponent )
Private
FRelatedComponents : TStringList;
FOwnerForm : TForm;
Protected
Procedure SetRelatedComponents( Value : TStringList );
Procedure DoTheWork( Value : Boolean );
Public
Constructor Create( AOwner : TComponent ); OverRide;
Destructor Destroy; OverRide;
Procedure EnableAll;
Procedure DisableAll;
Published
Property RelatedComponents : TStringList Read FRelatedComponents Write SetRelatedComponents;
End;
Procedure Register;
Implementation
Procedure TEnablerDisabler.SetRelatedComponents;
Var
IdX : Integer;
Begin
FRelatedComponents.Assign( Value );
If ( FRelatedComponents.Count 0 ) Then
For IdX := 0 To ( FRelatedComponents.Count - 1 ) Do
FRelatedComponents[ IdX ] := LowerCase( Trim( FRelatedComponents[ IdX ] ) );
End;
Procedure TEnablerDisabler.DoTheWork;
Var
IdX : Integer;
CompCount : Integer;
CurrentComp : TControl;
CompName : String;
Begin
CompCount := FOwnerForm.ComponentCount;
If ( FRelatedComponents.Count 0 ) Then
If ( CompCount 0 ) Then
For IdX := 0 To ( CompCount - 1 ) Do
If ( FOwnerForm.Components[ IdX ] Is TControl ) Then Begin
CurrentComp := TControl( FOwnerForm.Components[ IdX ] );
CompName := LowerCase( Trim( CurrentComp.Name ) );
If ( FRelatedComponents.IndexOf( CompName ) -1 ) Then
CurrentComp.Enabled := Value;
End;
End;
Constructor TEnablerDisabler.Create;
Begin
If ( Not( AOwner Is TForm ) ) Then
Raise Exception.Create( 'Owner of TEnablerDisabler must be a TForm component!' );
Inherited Create( AOwner );
FRelatedComponents := TStringList.Create;
FOwnerForm := TForm( AOwner );
End;
Destructor TEnablerDisabler.Destroy;
Begin
FRelatedComponents.Destroy;
Inherited Destroy;
End;
Procedure TEnablerDisabler.EnableAll;
Begin
DoTheWork( True );
End;
Procedure TEnablerDisabler.DisableAll;
Begin
DoTheWork( False );
End;
Procedure Register;
Begin
RegisterComponents( 'Christian', [ TEnablerDisabler ] );
End;
End.
---------------------------------------------------------------