Scientific editor in TTMSFMXGrid

My skills in Delphi are very rudimentary so apologies if this is an elementary question.


What would be the easiest way to set up a column in the TMS FireMonkey Grid whose cells accept only numbers in scientific notation such as -1.234E-05?

Hi, 


The grid columns can be configured to use an etNumeric or etSignedFloat Editor type but the E character is not numeric. You could however use a etSignedFloat editor type and then convert the value with FloatToStrF.

procedure TForm1.TMSFMXGrid1CellEditGetData(Sender: TObject; ACol,
  ARow: Integer; CellEditor: TFmxObject; var CellString: string);
var
  v: Extended;
begin
  if CellString <> '' then
  begin
    TryStrToFloat(CellString, v);
    CellString := FloatToStr(v);
  end;
end;

procedure TForm1.TMSFMXGrid1CellEditSetData(Sender: TObject; ACol,
  ARow: Integer; CellEditor: TFmxObject; var CellString: string);
begin
  CellString := FloatToStrF((CellEditor as TTMSFMXEdit).FloatValue, ffExponent, 8, 4);
end;

procedure TForm1.TMSFMXGrid1GetCellEditorType(Sender: TObject; ACol,
  ARow: Integer; var CellEditorType: TTMSFMXGridEditorType);
begin
  CellEditorType := etSignedFloatEdit;
end;

Please note that with this approach, the value in the editor will be shown as a normal float, while the grid cell will show the scientific notation. We will investigate if we can add a mode to allow scientific notation inside the editor.

Kind Regards, 
Pieter

Pieter Scheldeman2015-09-25 11:59:29

Pieter,


Your reply was helpful and educational. But I do need the user to be able to enter the value in scientific notation. Ideally, the user would be able to input the value in scientific or decimal notation, whichever was more convenient. If TMS can extend the editing modes available, that would be great. Perhaps in the interim, I could create a custom editor for this purpose, but I have very little idea how to go about it.

Hi, 


This, als explained in the previous post, is currently not possible and we will investigate here if we can improve this behavior.

Kind Regards, 
Pieter

Pieter, Coming back to your code above, I should like to allow the E character to enable scientific input so prefer not to use the etSignedFloat Editor type. If I use etEdit, it works nicely if the user enters a valid string in scientific format. If the user enters invalid characters, the entire string eventually gets rejected but it is nicer to prevent entry of invalid characters in the first place. Is there any way to specify valid characters for use with etEdit? I have tried using the editor etValidCharsEdit which correctly suppresses invalid characters but seems to always give zero for the floating point value at the code step 

CellString := FloatToStrF((CellEditor as TTMSFMXEdit).FloatValue, ffExponent, 8, 4);
Is there any way forward?

The FloatValue property isn't used with etValidChars so you will need to directly use the Text value instead of converting the FloatValue.


Kind Regards, 
Pieter