[英]Limit maximum text length of the inplace editor in TDBGrid
How can I limit the maximum text length of the inplace editor in TDBGrid
? 如何在
TDBGrid
限制inplace编辑器的最大文本长度? (Delphi Berlin) (德尔福柏林)
The Data Type is Float. 数据类型是Float。
The inplace editor in a TDBGrid
will update its content by calling TDBGrid
的inplace编辑器将通过调用更新其内容
procedure TInplaceEdit.UpdateContents;
begin
Text := '';
EditMask := Grid.GetEditMask(Grid.Col, Grid.Row);
Text := Grid.GetEditText(Grid.Col, Grid.Row);
MaxLength := Grid.GetEditLimit;
end;
Where GetEditMask
is implemented the following way: GetEditMask
的实现方式如下:
function TCustomDBGrid.GetEditMask(ACol, ARow: Longint): string;
begin
Result := '';
if FDatalink.Active then
with Columns[RawToDataColumn(ACol)] do
if Assigned(Field) then
Result := Field.EditMask;
end;
and GetEditLimit
like this: 和
GetEditLimit
像这样:
function TCustomDBGrid.GetEditLimit: Integer;
begin
Result := 0;
if Assigned(SelectedField) and (SelectedField.DataType in [ftString, ftWideString]) then
Result := SelectedField.Size;
end;
There you have multiple ways to get to the desired behavior I think. 在那里,您有多种方法可以达到我想要的行为。
Use TField
EditMask
property for the Field you want to restrict. 对要限制的字段使用
TField
EditMask
属性。 This will be returned by Grid.GetEditMask call. 这将由Grid.GetEditMask调用返回。 No need to inherit from TDBGrid and override anything.
无需从TDBGrid继承并覆盖任何内容。 Behavior can be controlled on a by-field-basis.
可以在逐场的基础上控制行为。
Create your own TDBGrid
descendant where you override GetEditLimit
to return a MaxLength for the inplace editor depending on SelectedField
创建自己的
TDBGrid
后代,在其中覆盖GetEditLimit
以根据SelectedField
返回GetEditLimit
编辑器的MaxLength
Code for approach 1 could look like this: 方法1的代码可能如下所示:
// Opening of dataset
...
DataSet.FieldByName('FloatField').EditMask := '00.00';
This will mask will require two digits before and after the decimal seperator. 这将掩盖在小数分隔符之前和之后需要两位数。 See
TEditMask
for more on masks. 有关
TEditMask
的更多信息,请参阅TEditMask
。
For approach 2: 方法2:
uses
Data.DB,
Vcl.DBGrids;
type
TMyDBGrid = class(TDBGrid)
protected
function GetEditLimit: Integer; override;
end;
implementation
{ TMyDBGrid }
function TMyDBGrid.GetEditLimit: Integer;
begin
Result := inherited GetEditLimit;
if (Result = 0) and Assigned(SelectedField) and (SelectedField.DataType = ftFloat) then
Result := 5; // Whatever you decide
end;
Like kobik suggests, you can then use this class as interposer class. 就像kobik建议的那样,你可以将这个类用作插入类。 To do this, add
TDBGrid = class(TMyDBGrid);
为此,添加
TDBGrid = class(TMyDBGrid);
in the unit you want to use that grid. 在您要使用该网格的单元中。 If you declared
TMyDBGrid
in the same unit you want to use it, make the type reference clear TMyDBGrid = class(Vcl.DBGrids.TDBGrid)
. 如果您在要使用它的同一单元中声明
TMyDBGrid
,请使类型引用清除TMyDBGrid = class(Vcl.DBGrids.TDBGrid)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.