I'm trying to write a ReadOnly property in a TComboBox, but I don't know
much about Messages and Win32API. So I looked into TCustomEdit.SetReadOnly
implementation to find out how it should be done (because I thought it's
implementation was the nearest one in VCL's source files examples close to
my purposes).
The component implementation was like below, but it didn't work. I traced
into it to debug it and I saw that HandleAllocated was always False. So I
commented the HandleAllocated's line to call SendMessage directly, but even
this way it didn't work.
Here it is:
TROComboBox = class(TComboBox)
private
FReadOnly: Boolean;
procedure SetReadOnly(Value: Boolean);
published
property ReadOnly: Boolean read FReadOnly write SetReadOnly default
False;
end;
procedure TROComboBox.SetReadOnly(Value: Boolean);
begin
if FReadOnly <> Value then
begin
FReadOnly := Value;
if HandleAllocated then
SendMessage(Handle, EM_SETREADONLY, Ord(Value), 0);
end;
end;
What am I doing wrong? Can anyone tell me how can I make this ReadOnly
property work?
I Appreciate any help.
Wilton Junior.
Have a look at this code fragment:
[..]
type
TMyMRUComboBox = class( TCustomComboBox )
private
[..]
FReadOnly: Boolean;
[..]
protected
[..]
procedure KeyPress( var Key: Char ); override;
[..]
procedure SetReadOnly( Value: Boolean ); virtual;
published
[..]
property ReadOnly: Boolean read FReadOnly write SetReadOnly default
False;
[..]
end;
procedure TMyMRUComboBox.KeyPress( var Key: Char );
begin
if Ord( Key ) = VK_RETURN then
begin
end
else
if Ord( Key ) = VK_ESCAPE then
begin
end
else
if not FReadOnly then
inherited KeyPress( Key );
end;
procedure TMyMRUComboBox.SetReadOnly( Value: Boolean );
begin
if FReadOnly <> Value then
begin
FReadOnly := Value;
if ( Style in [ csDropDown, csSimple ] ) and HandleAllocated then
SendMessage( EditHandle, EM_SETREADONLY, Ord( FReadOnly ), 0 );
end;
end;