?
Solved

Disable paste in a TEdit?

Posted on 2006-07-14
7
Medium Priority
?
815 Views
Last Modified: 2012-05-05
Hi,

Is there an easy way to disable pasting (using a mouse)?

I need to fix a program where OnKeyUp is used for input validation. Ofcourse when data is pasted using the mouse, this event is not generated. But the problem is that I cannot simply change all events to an OnChange. So a quick fix would be simply not allowing pasting.

cwwkie
0
Comment
Question by:cwwkie
[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
  • 3
  • 3
7 Comments
 
LVL 14

Author Comment

by:cwwkie
ID: 17108734
This is a simplified example of what is happening;

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    procedure Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure Edit2KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormShow(Sender: TObject);
    procedure EditChange(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  GlobalText1 : AnsiString;
  GlobalText2 : AnsiString;

implementation

{$R *.dfm}

procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  GlobalText1 := Edit1.Text;
  Edit2.Text := '';
end;

procedure TForm1.Edit2KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  GlobalText2 := Edit2.Text;
  Edit1.Text := '';
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  GlobalText1 := 'test';
  GlobalText2 := '';

  Edit1.Text := GlobalText1;
  Edit2.Text := GlobalText2;
end;

procedure TForm1.EditChange(Sender: TObject);
begin
  SendMessage(TEdit(Sender).Handle, WM_KEYUP, 0, 0);
end;

end.

When I simply call the keyup eventhandler from the onchange, it will also be called when the data is manually changed (Edit2.Text := GlobalText2;), which will clear Edit1 on it's turn. So I need to change all the eventhandlers to avoid this, but because this takes time, I like to have an alternative in the mean time.
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 2000 total points
ID: 17109024
You can subclass the edit and discard the message.

Regards,
Rusell

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

type
  TForm1            = class(TForm)
     Edit1:         TEdit;
     procedure      FormCreate(Sender: TObject);
  private
     // Private declarations
     FOldEditProc:  TWndMethod;
  protected
     // Protected declartions
     procedure      EditWndProc(var Message: TMessage);

  public
     // Public declarations
  end;

var
  Form1:            TForm1;

implementation
{$R *.DFM}

procedure TForm1.EditWndProc(var Message: TMessage);
begin

  if (Message.Msg = WM_PASTE) then
     Message.Result:=1
  else
     FOldEditProc(Message);

end;

procedure TForm1.FormCreate(Sender: TObject);
begin

  FOldEditProc:=Edit1.WindowProc;
  Edit1.WindowProc:=EditWndProc;

end;

end.
0
 
LVL 14

Author Comment

by:cwwkie
ID: 17109200
Yes, that will do it.
Thanks!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Expert Comment

by:House_of_Dexter
ID: 17109292
in your 2nd problem you have a circular reference going on with your events...

Edit1.keyup calls edit1.change which calls keyup and so on and so on...

There's 2 ways to go about this...

one disable the event

procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
var
  aEventHolder: TNotifyEvent;
begin
  aEventHolder := Edit1.OnChange;
  try
    GlobalText1 := Edit1.Text;
    Edit2.Text := '';
  finally
    Edit1.OnChange := aEventHolder;
  end
end;


or better Create a State varible
I usually a Set

//TFormStateType ...use this if you need to know what the state is...
{CAREFUL...if you add to this...change TFormStateType}
const
  cEditKeyUp = 0;
  cEditChange = 1;
....add state that you are interested in here...
  cSomeStateChange =9;
 
 
cFormStateSet = [cEditKeyUp..cSomeStateChange ]

type
    TFormStateType =  cIsInitialize..cIsUpdateStatus;
    TFormStateSet = Set of TFormStateType;

//ususally declare something like this in Private of Form

  FFormState: TFormStateSet ;

procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  GlobalText1 := Edit1.Text;
  Edit2.Text := '';
  FFormState := FFormState + cEditKeyUp ;
end;

procedure TForm1.EditChange(Sender: TObject);
begin
  if cEditKeyUp in FFormState then
    FFormState :=  FFormState - cEditKeyUp
  else
    SendMessage(TEdit(Sender).Handle, WM_KEYUP, 0, 0);
end;

0
 
LVL 6

Expert Comment

by:House_of_Dexter
ID: 17109439
procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
var
  aEventHolder: TNotifyEvent;
begin
  aEventHolder := Edit1.OnChange;
  try
   Edit1.OnChange := nil;  <--forgot this
    GlobalText1 := Edit1.Text;
    Edit2.Text := '';
  finally
    Edit1.OnChange := aEventHolder;
  end
end;
0
 
LVL 14

Author Comment

by:cwwkie
ID: 17111804
> in your 2nd problem you have a circular reference going on with your events...

But If I disable pasting, I don't need the onchange, and have no circular reference. As soon as I have properly implemeted a good OnChange for all edit's, I can enable pasting (and cutting) again.
0
 
LVL 6

Expert Comment

by:House_of_Dexter
ID: 17114413
True...but if you ever implement an OnChange you will need to do something like I showed...
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

771 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