Board index » delphi » Limit user input length at a DBGrid cell

Limit user input length at a DBGrid cell

Hi there,

Is there a way to limit user input length inside a DBGrid cell for float
cells? I can type forever no matter which EditMask the TField is using.

Thanks in advance,

-Jorge

 

Re:Limit user input length at a DBGrid cell


Yes there is.
 use the commonly known OnKeyPress event ...
in that event you can check the current length of  active cell et.c..
if the length is exceeding and the charactor being entered would add to its
length then set the charactor to #0;
for example in the OnKeyPress event
 if  Char in [' '..#255] and Length(MySelectedCellString) >= MyLimit THen
  Char := #0;
etc..
Quote
Jorge Alvarez wrote:
> Hi there,

> Is there a way to limit user input length inside a DBGrid cell for float
> cells? I can type forever no matter which EditMask the TField is using.

> Thanks in advance,

> -Jorge

Re:Limit user input length at a DBGrid cell


Quote
"Jorge Alvarez" <correosch...@hotmail.com> wrote in message

news:3e774653$1@newsgroups.borland.com...

Quote
> Hi there,

> Is there a way to limit user input length inside a DBGrid cell for float
> cells? I can type forever no matter which EditMask the TField is using.

This has had only a little light exercise so give it a good testing.  This
gives about the simplest coding but it isn't particularly elegant as it fires
at every keystroke.  However, unless you know some really fast typists this
won't be an issue.
Controls[0] is the InplaceEditor, by the time the dataset is in edit state we
can be sure it has been created - a one-time event.  Limitation - only works
with keyed input, handling pasted text is more convoluted.

procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (TDBGrid(Sender).SelectedField.DataType = ftFloat)
    AND (TDBGrid(Sender).DataSource.DataSet.State in [dsEdit, dsInsert]) then
    TEdit(TDBGrid(Sender).Controls[0]).MaxLength := 5;
end;

--
Regards,
Chris Luck.

Other Threads