Page 1 of 1

Memo, SelText & Undo

PostPosted: July 29th, 2003, 8:42 am
by Johnny_Bit
Why when I assign SelText with Memo1.Seltext:=FormatDateTime('hh/nn/ss', Now) CanUndo=False? I've tried to add Memo1.Modified:=True; but CanUndo is still false. So i red SetSelText procedure from StdCtrls and lparam=0, but in hel borlan says thal ord(False{BOOL False, not Boolean False})=0 so I wrote custom component that sends message wit lparam=10 and still canundo is false. What i do wrong or what I should do to make undo possible (maybe make list of undo operations, but that will be too much for my little program)?

PostPosted: July 29th, 2003, 7:27 pm
by Kambiz
The following code works.

Code: Select all
S := FormatDateTime('hh/nn/ss', Now);
SendMessage(Memo1.Handle, EM_REPLACESEL, 1, LPARAM(PChar(S)));

Cheers,
Kambiz

PostPosted: July 30th, 2003, 11:27 am
by Johnny_Bit
This is very good, but this isn't good in my program (it happends) so i will contretize it a bit:

How to make custom component based on TMemo that seltext property will work properly (canundo)?

One more thing: can you test it:
Code: Select all
unit HMemo;

interface

uses
  Windows, Messages, SysUtils, Classes, Controls, StdCtrls;

type
  THMemo = class(TMemo)
  private
    procedure SetSelText(const Value: string); reintroduce;
  protected
    { Protected declarations }
  public
    procedure SetSelTextBuf(Buffer: PChar);
  published
    constructor Create(AOwner: TComponent); reintroduce;
    property SelText read GetSelText write SetSelText;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('HAKGERSoft Components', [THMemo]);
end;

{ THMemo }

constructor THMemo.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
end;

procedure THMemo.SetSelText(const Value: string);
begin
  SendMessage(Handle, EM_REPLACESEL, 1, Longint(PChar(Value)));
end;

procedure THMemo.SetSelTextBuf(Buffer: PChar);
begin
  SendMessage(Handle, EM_REPLACESEL, 1, LongInt(Buffer));
end;

end.


And one more thing: Does typecasting will THMemo to TCustomMemo will change THMemo's SetSelText to TCustomEdit's SetSelText?

PostPosted: July 31st, 2003, 9:15 pm
by Kambiz
The THMemo will work properly, however the SelText property will work like the base class. Because the SetSelText method of the ancestor is a private and static method.

If you typecast a class to its base or child class and then call a virtual method, the typecast has no effect (because of polymorphism). However, for the static methods, method of typecast will be called.

Kambiz

PostPosted: August 2nd, 2003, 4:50 pm
by Johnny_Bit
OK, then what I Should do to make THMemo to using my SetSelTtext? reintroduce doesn't isn't enought?

PostPosted: August 2nd, 2003, 5:15 pm
by Kambiz
Tehe reintreduce directive will not work because the base method is private.

Why you don't use a procedure like the following one?

Code: Select all
procedure SetSelText(Edit: TCustomEdit; const S: String);
begin
   SendMessage(Edit.Handle, EM_REPLACESEL, 1, LPARAM(PChar(S)));
end;

Kambiz

PostPosted: August 3rd, 2003, 8:30 am
by Johnny_Bit
These seems like a good way, but i'm thinking about this:
Code: Select all
unit HMemo;

interface

uses
  Windows, Messages, SysUtils, Classes, Controls, StdCtrls;

type
  THMemo = class(TMemo)
  private
    procedure SetSelTextMy(const Value: string);
  protected
    { Protected declarations }
  public
    procedure SetSelTextBuf(Buffer: PChar);
  published
    constructor Create(AOwner: TComponent); reintroduce;
    property SelText read GetSelText write SetSelTextMy;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('HAKGERSoft Components', [THMemo]);
end;

{ THMemo }

constructor THMemo.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
end;

procedure THMemo.SetSelTextMy(const Value: string);
begin
  SendMessage(Handle, EM_REPLACESEL, 1, Longint(PChar(Value)));
end;

procedure THMemo.SetSelTextBuf(Buffer: PChar);
begin
  SendMessage(Handle, EM_REPLACESEL, 1, LongInt(Buffer));
end;

end.


will it work as i want?

PostPosted: August 3rd, 2003, 12:05 pm
by Kambiz
You should impelement the GetSelText method also, because in base class it is private.

Kambiz

PostPosted: August 4th, 2003, 10:59 am
by Johnny_Bit
It doesn't matters, it works! Now I know how make what I want.