diff --git a/lcl/valedit.pas b/lcl/valedit.pas index 48a545b442..ccd422c951 100644 --- a/lcl/valedit.pas +++ b/lcl/valedit.pas @@ -5,7 +5,7 @@ unit ValEdit; interface uses - Classes, SysUtils, Grids, LResources; + Classes, SysUtils, Grids, LResources, Dialogs, LazUtf8; type @@ -78,6 +78,7 @@ type procedure SetCells(ACol, ARow: Integer; const AValue: string); override; procedure SetEditText(ACol, ARow: Longint; const Value: string); override; procedure TitlesChanged(Sender: TObject); + function ValidateEntry(const ACol,ARow:Integer; const OldValue:string; var NewValue:string): boolean; override; public constructor Create(AOwner: TComponent); override; destructor Destroy; override; @@ -202,6 +203,10 @@ type end; +const + //ToDo: Make this a resourcestring in lclstrconsts unit, once we are satisfied with the implementation of validating + rsVLEDuplicateKey = 'Duplicate Key:'+LineEnding+'A key with name "%s" already exists at column %d'; + procedure Register; implementation @@ -501,9 +506,6 @@ procedure TValueListEditor.SetEditText(ACol, ARow: Longint; const Value: string) begin inherited SetEditText(ACol, ARow, Value); Cells[ACol, ARow] := Value; - // ToDo: There must be a check for duplicate keys but it must - // not be triggered while the user is typing. - // The error must be postponed until user moves to other cell. end; procedure TValueListEditor.TitlesChanged(Sender: TObject); @@ -514,6 +516,30 @@ begin Invalidate; end; +function TValueListEditor.ValidateEntry(const ACol, ARow: Integer; + const OldValue: string; var NewValue: string): boolean; +var + Index, i: Integer; +begin + Result := inherited ValidateEntry(ACol, ARow, OldValue, NewValue); + if ((ACol - FixedCols) = 0) then + begin//Check for duplicate key names (only in "Key" column) + Index := ARow - FixedRows; + for i := 0 to FStrings.Count - 1 do + begin + if (Index <> i) then + begin + if (Utf8CompareText(FStrings.Names[i], NewValue) = 0) then + begin + Result := False; + ShowMessage(Format(rsVLEDuplicateKey,[NewValue, i + FixedRows])); + if Editor is TStringCellEditor then TStringCelleditor(Editor).SelectAll; + end; + end; + end; + end; +end; + class procedure TValueListEditor.WSRegisterClass; begin // RegisterPropertyToSkip(Self, 'SomeProperty', 'VCL compatibility property', '');