Solved

Preventing Cut and Paste in edit box

Posted on 1998-08-10
9
192 Views
Last Modified: 2010-04-04
Hi all, I have a program that tests a users input after a lesson and I need to be able to prevent the users from cutting and pasting from the lesson portion to the edit box in the testing.How do I do this without eliminating the ability to use the shift and ctrl keys?(the ctrl isnt important but they may have to type in uppercase characters at times) Any ideas? Any help is greatly appreciated....
0
Comment
Question by:GabeinOZ
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 1361860
GabeinOZ,

 make a descendant of TEdit and trap the messages for WM_PASTE, WM_CUT, WM_COPY. When you've trapped them, just do nothing! Don't call inherited.

Zif.
0
 
LVL 3

Expert Comment

by:vladika
ID: 1361861
I wrote small component TCutPasteMonitor.
It can handle any TWinControl, not only TEdit, but any its descendant
TMemo, TMaskEdit ets.
Also you can modify it for your purpose.
Enjoy :)

unit CutPasteMnt;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TCutPasteMonitor = class(TComponent)
  private
    FCanCut, FCanCopy, FCanPaste: Boolean;
    FControl: TWinControl;
    FWndProcAdd : Pointer;
    ObjectInstance : Pointer;
    procedure SetControl(Value : TWinControl);
  protected
    procedure WndProc(var Message: TMessage);
    function Execute(Message: TMessage) : Boolean; virtual;
  public
    constructor Create(AOwner : TComponent); override;
    destructor Destroy; override;
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  published
    property CanCut: Boolean read FCanCut write FCanCut;
    property CanCopy: Boolean read FCanCopy write FCanCopy;
    property CanPaste: Boolean read FCanPaste write FCanPaste;
    property Control: TWinControl read FControl write SetControl;
  end;

procedure Register;

implementation

{ TCutPasteMonitor }

constructor TCutPasteMonitor.Create(AOwner : TComponent);
begin
  inherited Create(AOwner);
  ObjectInstance := MakeObjectInstance(WndProc);
end;

destructor TCutPasteMonitor.Destroy;
begin
  if (ObjectInstance <> nil) then
    FreeObjectInstance(ObjectInstance);
  inherited Destroy;
end;

procedure TCutPasteMonitor.WndProc(var Message: TMessage);
begin
  if not Execute(Message) then
  with Message do
    Result := CallWindowProc(FWndProcAdd, FControl.Handle, Msg, WParam, LParam);
end;

procedure TCutPasteMonitor.Notification(AComponent: TComponent; Operation: TOperation);
begin
  inherited;
  if(AComponent = FControl) And (Operation = opRemove) then
    FControl := nil;
end;

procedure TCutPasteMonitor.SetControl(Value : TWinControl);
begin
  if(FControl = Value) then Exit;
  if(FControl <> nil) and (FWndProcAdd <> nil) then
  begin
    SetWindowLong(FControl.Handle, GWL_WNDPROC, LongInt(FWndProcAdd));
    FWndProcAdd := nil;
  end;
  FControl := Value;
  if(FControl <> nil) and not (csDesigning in FControl.ComponentState) then
  begin
    FWndProcAdd := Pointer(GetWindowLong(FControl.Handle, GWL_WNDPROC));
    SetWindowLong(FControl.Handle, GWL_WNDPROC, LongInt(ObjectInstance));
  end;
end;

function TCutPasteMonitor.Execute(Message: TMessage) : Boolean;
begin
  Result := True;
  if not FCanCut and (Message.Msg = WM_CUT) then Exit;
  if not FCanCopy and (Message.Msg = WM_COPY) then Exit;
  if not FCanPaste and (Message.Msg = WM_PASTE) then Exit;
  Result := False;
end;

procedure Register;
begin
  RegisterComponents('ExpertsExchange', [TCutPasteMonitor]);
end;

end.
0
 
LVL 3

Expert Comment

by:vladika
ID: 1361862
It's another approach to a problem then ZifNaf proposes.
Instead making descendant of each control
we are subclassing existing controls (and their existing and future descendant).

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:vladika
ID: 1361863
SORRY!!! ZifNab instead of ZifNaf

0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1361864
vladika, no problem with ZifNaf... BanZif :-)
0
 
LVL 3

Expert Comment

by:vladika
ID: 1361865
I forgot explain how to use my component.
Set TEdit, TMemo or other TWinControl on the form.
Set TCutPasteMonitor on the form.
Set CutPasteMonitor.Control property.
Set CanCut, CanCopy and CanPaste properties.

0
 

Author Comment

by:GabeinOZ
ID: 1361866
thanks for the help zif but I tried Banzif's ( :P) component and it works perfectly....thanks for your answer though...
0
 
LVL 3

Accepted Solution

by:
vladika earned 50 total points
ID: 1361867
Hi, GabeinOZ

According to your letter
> this is GabeinOZ from e.e.,   you added a comment to my question yesterday and
> I used your answer rather than the one provided by Zifnab
> so answer the q again so I can give you the points...
> Thanks again for your answer....works brilliantly!!! :)

I post my "official" answer (see below/above)

0
 

Author Comment

by:GabeinOZ
ID: 1361868
thanks again man...your component is sweet :)
catch ya next time...

Gabe
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question