Memo, SelText & Undo

Please discuss general Delphi programming topics here.

Memo, SelText & Undo

Postby Johnny_Bit » July 29th, 2003, 8:42 am

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)?
Johnny_Bit
VIP Member
VIP Member
 
Posts: 455
Joined: June 15th, 2003, 9:56 am

Postby Kambiz » July 29th, 2003, 7:27 pm

The following code works.

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

Cheers,
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2408
Joined: March 7th, 2003, 7:10 pm

Postby Johnny_Bit » July 30th, 2003, 11:27 am

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?
Johnny_Bit
VIP Member
VIP Member
 
Posts: 455
Joined: June 15th, 2003, 9:56 am

Postby Kambiz » July 31st, 2003, 9:15 pm

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
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2408
Joined: March 7th, 2003, 7:10 pm

Postby Johnny_Bit » August 2nd, 2003, 4:50 pm

OK, then what I Should do to make THMemo to using my SetSelTtext? reintroduce doesn't isn't enought?
Johnny_Bit
VIP Member
VIP Member
 
Posts: 455
Joined: June 15th, 2003, 9:56 am

Postby Kambiz » August 2nd, 2003, 5:15 pm

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
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2408
Joined: March 7th, 2003, 7:10 pm

Postby Johnny_Bit » August 3rd, 2003, 8:30 am

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?
Johnny_Bit
VIP Member
VIP Member
 
Posts: 455
Joined: June 15th, 2003, 9:56 am

Postby Kambiz » August 3rd, 2003, 12:05 pm

You should impelement the GetSelText method also, because in base class it is private.

Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2408
Joined: March 7th, 2003, 7:10 pm

Postby Johnny_Bit » August 4th, 2003, 10:59 am

It doesn't matters, it works! Now I know how make what I want.
Johnny_Bit
VIP Member
VIP Member
 
Posts: 455
Joined: June 15th, 2003, 9:56 am


Return to Delphi Programming

Who is online

Users browsing this forum: No registered users and 2 guests

cron