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

StringGrid: how to set MaxLength for a specific Column

1,416 views
Skip to first unread message

Gerhard Zampich

unread,
Jul 4, 2008, 5:54:00 AM7/4/08
to
Hello,

I am trying to limit the data entry in a column of a stringgrid to a
specific MaxLength.
I have done the following:

type
TStringGridCracker = class(TStringGrid)
end;

procedure TMainForm.GridSetEditText(Sender: TObject; ACol,
ARow: Integer; const Value: String);
begin
if ( ACol = 5 )
then with
TStringGridCracker(Sender)

do if (InplaceEditor <> nil
)
then TEdit(InplaceEditor).MaxLength := 55;
end;

This works good, with one exception:
When the cell at column 5 holds already 55 chars when the cell is
selected, one can enter a 56th character.
So it seems that the SetEditText event is to late to set maxlength.
What is the correct way to set TEdit(InplaceEditor).MaxLength?

Regards
Gerhard

Chris Luck

unread,
Jul 4, 2008, 1:38:56 PM7/4/08
to
Gerhard Zampich wrote:
> What is the correct way to set TEdit(InplaceEditor).MaxLength?

I don't know about it being correct, and it certainly isn't elegant,
but you might try this -

procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (StringGrid1.Col = 5) and (StringGrid1.ControlCount > 0) then
TEdit(StringGrid1.Controls[0]).MaxLength := 55;
end;

--
Regards,
Chris Luck

Gerhard Zampich

unread,
Jul 5, 2008, 7:00:07 AM7/5/08
to
Hello Chris,

>
> procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;
> Shift: TShiftState);
> begin
> if (StringGrid1.Col = 5) and (StringGrid1.ControlCount > 0) then
> TEdit(StringGrid1.Controls[0]).MaxLength := 55;
> end;

thank you for your answer, but with your solution I need to set
MaxLength also in the MouseDown event.
Otherwise one can Paste more chars into the cell than MaxLength.
I solved it now this way:

UWM_SETMAXLENGTH = WM_USER + 406;
....
procedure UWMSETMAXLENGTH(var Msg: TMessage); message UWM_SETMAXLENGTH;
....
procedure TMainForm.GridGetEditMask(Sender: TObject; ACol,
ARow: Integer; var Value: String);
begin
if ( aCol = 5 )
then PostMessage(Self.Handle
,UWM_SETMAXLENGTH
,{WParam=}55)

,{LParam=}LongInt(TStringGridCracker(Sender).InplaceEditor));

end;
....
procedure TMainForm.UWMSETMAXLENGTH(var Msg: TMessage);
begin
TEdit(MSG.LParam).MaxLength := Integer(MSG.WParam);
end;

Are there any concerns about this solution?

Regards
Gerhard

Chris Luck

unread,
Jul 5, 2008, 11:01:32 PM7/5/08
to
Gerhard Zampich wrote:
> Hello Chris,
>> procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;
>> Shift: TShiftState);
>> begin
>> if (StringGrid1.Col = 5) and (StringGrid1.ControlCount > 0) then
>> TEdit(StringGrid1.Controls[0]).MaxLength := 55;
>> end;
>
> thank you for your answer, but with your solution I need to set MaxLength
> also in the MouseDown event. Otherwise one can Paste more chars into the
> cell than MaxLength.


Ok, move those lines into OnDrawCell and you'll find it covers pasting too!
(DefaultDrawing true or false)

--
Regards,
Chris Luck

Remy Lebeau (TeamB)

unread,
Jul 6, 2008, 3:49:21 AM7/6/08
to

"Gerhard Zampich" <ma...@Zampich.de> wrote in message
news:486d...@newsgroups.borland.com...

> What is the correct way to set TEdit(InplaceEditor).MaxLength?

Derive a new component from TStringGrid and override the virtual
GetEditLimit() method.


Gambit


Gerhard Zampich

unread,
Jul 6, 2008, 7:38:23 AM7/6/08
to
Hello Chris,

> Ok, move those lines into OnDrawCell and you'll find it covers pasting
> too!
> (DefaultDrawing true or false)

this seems to be the easiest solution; no cracker class is needed, no
Message and no override.
My final code looks now this way:

procedure TMainForm.GridDrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);


begin
if ( aCol = 5 )

then with TStringGrid(Sender)
do if ( ControlCount > 0)
then TEdit(Controls[0]).MaxLength := 55;
end;

Thank you Chris and Remy.

But there is still one thing missing:

A standard TEdit Beeps if one tries to type more characters into it
than MaxLen allows. The InplaceEditor of the Grid did not Beep.

Has anybody a solution for that?

Regards
Gerhard

Descartes AT -REMOVE-THIS- DOT

unread,
Jul 6, 2008, 4:28:51 PM7/6/08
to

"Gerhard Zampich" wrote

> Hello,
>
> I am trying to limit the data entry in a column of a stringgrid to a
> specific MaxLength.
>

Gerhard,

Consider the OnGetEditMask event:

procedure TForm1.StringGrid1GetEditMask(Sender: TObject; ACol, ARow:
Integer; var Value: string);
begin
if ACol = 5 then
value := 'ccccccccccccccc;0; ' // limit to 15 arbitrary
characters
else
value := '';
end;

See TEditMask help for details.

HTH
Descartes


Gerhard Zampich

unread,
Jul 7, 2008, 4:04:39 AM7/7/08
to
Hello Descartes,

>Consider the OnGetEditMask event:
>
>procedure TForm1.StringGrid1GetEditMask(Sender: TObject; ACol, ARow:
>Integer; var Value: string);
>begin
> if ACol = 5 then
> value := 'ccccccccccccccc;0; ' // limit to 15 arbitrary
>characters
> else
> value := '';
>end;
>

I have tried that, but is not the same as MaxLength. The edit is
completeley different.

Regards
Gerhard

Remy Lebeau (TeamB)

unread,
Jul 7, 2008, 2:17:34 PM7/7/08
to

"Gerhard Zampich" <ma...@Zampich.de> wrote in message
news:4871...@newsgroups.borland.com...

> I have tried that, but is not the same as MaxLength.
> The edit is completeley different.

See my other reply.


Gambit


Gerhard Zampich

unread,
Jul 8, 2008, 4:35:04 AM7/8/08
to
Hello Remy

>Derive a new component from TStringGrid and override the virtual
>GetEditLimit() method.
>

Does that solve the missing BEEP when the limit is reached?

Regards
Gerhard

0 new messages