簡體   English   中英

限制TDBGrid中的inplace編輯器的最大文本長度

[英]Limit maximum text length of the inplace editor in TDBGrid

如何在TDBGrid限制inplace編輯器的最大文本長度? (德爾福柏林)

數據類型是Float。

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;

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;

GetEditLimit像這樣:

function TCustomDBGrid.GetEditLimit: Integer;
begin
  Result := 0;
  if Assigned(SelectedField) and (SelectedField.DataType in [ftString, ftWideString]) then
    Result := SelectedField.Size;
end;

在那里,您有多種方法可以達到我想要的行為。

  • 對要限制的字段使用TField EditMask屬性。 這將由Grid.GetEditMask調用返回。 無需從TDBGrid繼承並覆蓋任何內容。 可以在逐場的基礎上控制行為。

  • 創建自己的TDBGrid后代,在其中覆蓋GetEditLimit以根據SelectedField返回GetEditLimit編輯器的MaxLength

方法1的代碼可能如下所示:

// Opening of dataset
...
DataSet.FieldByName('FloatField').EditMask := '00.00';

這將掩蓋在小數分隔符之前和之后需要兩位數。 有關TEditMask的更多信息,請參閱TEditMask

方法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;

就像kobik建議的那樣,你可以將這個類用作插入類。 為此,添加TDBGrid = class(TMyDBGrid); 在您要使用該網格的單元中。 如果您在要使用它的同一單元中聲明TMyDBGrid ,請使類型引用清除TMyDBGrid = class(Vcl.DBGrids.TDBGrid)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM