SysUtilsStrToFloat (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example uses a form with a combo box, two list boxes, an edit control, and a button. When you select a conversion family from the combo box, this causes the list boxes to fill with the conversion types registered with that family. When you click the button, a message box appears, showing the value in the edit control converted from the units selected in the first list box to the units selected in the second list box. Note: If you have not registered your own conversion families, include the stdconvs unit in your uses clause to use the standard conversion families.

Code

{The following OnCreate event handler for the form initializes
the combo box and list boxes.}

procedure TForm1.FormCreate(Sender: TObject);
var
  FamilyList: TConvFamilyArray;
  i: Integer;
begin
  GetConvFamilies(FamilyList);
  for i := 0 to (Length(FamilyList) - 1) do
    ComboBox1.Items.Add(ConvFamilyToDescription(FamilyList[i]));
  ComboBox1.ItemIndex := 0; { Select the first item. }
  ComboBox1Select(ComboBox1); { Trigger the event to initialize the lists. }
end;


{The following OnClick event handler for the button reads the
value entered in the edit box and converts it between the
measurement units selected in the list boxes.}

procedure TForm1.Button1Click(Sender: TObject);
var
  newVal: Double;
  CurFamily: TConvFamily;
  FromType, ToType: TConvType;
begin
  DescriptionToConvFamily(ComboBox1.Items[ComboBox1.ItemIndex], CurFamily);
  DescriptionToConvType(CurFamily, ListBox1.Items[ListBox1.ItemIndex], FromType);
  DescriptionToConvType(CurFamily, ListBox2.Items[ListBox2.ItemIndex], ToType);
  newVal := Convert(StrToFloat(Edit1.Text), FromType, ToType);
  ShowMessage(Format('%g %s', [newVal, ConvTypeToDescription(ToType)]));
end;


{The following OnSelect event handler for the combo box
initializes the list boxes with the registered conversion
types for the selected family.}

procedure TForm1.ComboBox1Select(Sender: TObject);
var
  TypeList: TConvTypeArray;
  i: Integer;
  CurFamily: TConvFamily;
  Description: string;
begin
  Description := ComboBox1.Items[ComboBox1.ItemIndex];
  if DescriptionToConvFamily(Description, CurFamily) then
  begin
    GetConvTypes(CurFamily, TypeList);
    ListBox1.Items.Clear;
    ListBox2.Items.Clear;
    for i := 0 to Length(TypeList) -1 do
      ListBox1.Items.Add(ConvTypeToDescription(TypeList[i]));
    ListBox2.Items := ListBox1.Items;
    ListBox1.ItemIndex := 0;
    ListBox2.ItemIndex := 0;
  end;
end;

Uses