Delphi sets an Edit Mask without a fixed character length

I have an object of type TValueListEditor that contains certain function parameters in the Key column and input for the corresponding column of value to test the function. I added an Edit Mask to enter a value depending on the type of data that should be a parameter. For example, the parameter Num1 is of type int, so the input should be only a number, but since I do not know the exact number of digits in advance, is there a way to specify EditMask without a fixed character length?

If you look at the code below, if I need a value of type float, I must have a point, but I do not want the point to be predetermined 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 need to implement something like regex in EditMask? Or is there another way to implement value input validation?

+5
source share
1 answer

In the TItemProp.EditMask documentation :

Validation using the EditMask property is character-based .

This way you can use fixed-width masks. This means that you must indicate where the decimal point will be, and how many leading and trailing digits to accept.

Use TValueListEditor.OnValidate :

Occurs when focus is shifted from the cell in the value list editor.

Write an OnValidate event handler to check all the changes that the user enters into the cell before the focus leaves it. OnValidate gives applications the ability to provide more validation than the EditMask property of the corresponding TItemProp object can provide .

OnValidate only occurs if the user has edited the value of the cell that is about to lose focus. The OnValidate event handler can check the value provided by the user, and if it is unacceptable, throw an exception.

For instance:

 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; 
+5
source

Source: https://habr.com/ru/post/1260650/


All Articles