Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi set Edit Mask with no fixed length of characters

I have a object of type TValueListEditor that contains certain parameters of a function in the Key column, and an input for the appropriate value column to test the function. I've added an Edit Mask to the Value Input depending on the type of data the parameter should be. For instance parameter Num1 is of type int, so the input would have to be only numbers, but since I don't know the exact number of digits in advance, is there a way to specify the EditMask without fixed length of characters?

If you look at the code below, if I need a value of type float, I have to have a point, but I don't want the point to be predefined at that exact position.

    if parser.sParams.Values[parser.sParams.Names[i]]='float' then
    begin
    lstValParamValues.ItemProps[parser.sParams.Names[i]].EditMask:='#########.#';
    end

Maybe I should implement something like regex on the EditMask? Or is there another way to implement validation of the value input?

like image 250
e.l.g.o. Avatar asked Dec 01 '16 14:12

e.l.g.o.


1 Answers

Per the TItemProp.EditMask documentation:

Validation using the EditMask property is performed on a character-by-character basis.

So you can only use fixed-width masks. Which means you have to specify where the decimal point is going to be, and how many leading and trailing digits to accept.

Consider using the the TValueListEditor.OnValidate event instead:

Occurs when focus shifts away from a cell in the value list editor.

Write an OnValidate event handler to validate any edits the user enters in a cell before focus leaves it. OnValidate gives applications an opportunity to provide more validation than the EditMask property of the corresponding TItemProp object can supply.

OnValidate only occurs if the user edited the value of the cell that is about to lose focus. The OnValidate event handler can verify the value the user supplied, and if it is not acceptable, raise an exception.

For example:

uses
  SysConsts;

procedure TMyForm.lstValParamValuesValidate(Sender: TObject; ACol, ARow: Integer; const KeyName: String; const KeyValue: String);
var
  ValueType: string;
  sIgnored: Single;
  dIgnored: Double;
begin
  if KeyValue = '' then Exit;

  ValueType := parser.sParams.Values[KeyName];

  if ValueType = 'int' then
    StrToInt(KeyValue)

  else if ValueType = 'float' then
  begin
    if not TryStrToFloat(KeyValue, sIgnored) then
      raise EConvertError.CreateFmt(SInvalidFloat, [KeyValue]);
  end

  else if ValueType = 'double' then
  begin
    if not TryStrToFloat(KeyValue, dIgnored) then
      raise EConvertError.CreateFmt(SInvalidFloat, [KeyValue]);
  end

  // etc...
end;
like image 163
Remy Lebeau Avatar answered Sep 29 '22 05:09

Remy Lebeau