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
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
>
> 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
Ok, move those lines into OnDrawCell and you'll find it covers pasting too!
(DefaultDrawing true or false)
--
Regards,
Chris Luck
> What is the correct way to set TEdit(InplaceEditor).MaxLength?
Derive a new component from TStringGrid and override the virtual
GetEditLimit() method.
Gambit
> 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
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
>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
> I have tried that, but is not the same as MaxLength.
> The edit is completeley different.
See my other reply.
Gambit
>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