Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to write a ReadOnly property in a TComboBox?

232 views
Skip to first unread message

Wilton Junior

unread,
Jun 19, 2003, 11:17:26 PM6/19/03
to
Hi Everybody,

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.

Michael Fritz

unread,
Jun 20, 2003, 2:34:16 AM6/20/03
to
Wilton,

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;


0 new messages