In the Extending TCustomEditControls (TMemo, TRichEdit) with a ViewOnly property a usage of class helpers in Delphi for Win32 was "put to test".
Using class helpers I was able to add a "ViewOnly" property to any TCustomEditControl descendant like TEdit, TMemo or TRichEdit.
ViewOnly property is a mix of Enabled and ReadOnly where the control with ViewOnly set to true will not be grayed, will be read only but a user will not be able to set the input focus to the control.
ViewOnly for TWinControl descendants
In the VCL hierarchy the TWinControl type is the base class for all controls that are wrappers for Windows screen objects: TButton, TEdit, TMemo, TListBox, TCheckBox, etc.Controls that extend the TWinControl class can receive user input focus and can serve as a container for other controls.
Here's the interface of the TWinControlHelper (implementation and the full source code available for download).
TWinControlHelper = class Helper for TWinControl
strict private
function GetHookControl : TObject;
function GetViewOnly: Boolean;
procedure SetViewOnly(const Value: Boolean) ;
function GetCanViewOnly: Boolean;
procedure SetCanViewOnly(const Value: Boolean) ;
public
property ViewOnly : Boolean read GetViewOnly write SetViewOnly;
property CanViewOnly : Boolean read GetCanViewOnly write SetCanViewOnly;
end;
Here's how to use the new ViewOnly property - a class helpers gem:
- Take a form, new (empty) or existing.
- If there are no controls on the form, drop some.
- Add ViewOnlyU to the uses clause
- Add a button on your form, name the button btnViewOnly.
- Inside the form's OnCreate event handler add a line of code: btnViewOnly.CanViewOnly := False;
- Implement the OnClick event handler for the btnViewOnly as:
procedure TTestForm.btnViewOnlyClick(Sender: TObject) ; begin ViewOnly := not ViewOnly; end;
