home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2002 September / PCPlus_193_Laplink2000.iso / Prog / delphi / convert / conv.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2002-04-26  |  3.1 KB  |  111 lines

  1. unit conv;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, StdCtrls,
  8.   convutils,stdconvs;
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.     ListBox1: TListBox;
  13.     ListBox2: TListBox;
  14.     Edit1: TEdit;
  15.     Button1: TButton;
  16.     ComboBox1: TComboBox;
  17.     msglabel: TLabel;
  18.     Label1: TLabel;
  19.     Label2: TLabel;
  20.     Label3: TLabel;
  21.     Label4: TLabel;
  22.     ResultEdit: TEdit;
  23.     Label5: TLabel;
  24.     procedure FormCreate(Sender: TObject);
  25.     procedure ComboBox1Select(Sender: TObject);
  26.     procedure Button1Click(Sender: TObject);
  27.   private
  28.     { Private declarations }
  29.   public
  30.     { Public declarations }
  31.     procedure ShowMsg( msg : string );
  32.     procedure ShowResult(fromval,toval : Double; FromType,ToType :TConvType);
  33.   end;
  34.  
  35. var
  36.   Form1: TForm1;
  37.  
  38. implementation
  39.  
  40. {$R *.dfm}
  41.  
  42. procedure TForm1.ShowMsg( msg : string );
  43. begin
  44.    msglabel.Caption := msg;
  45. end;
  46.  
  47. procedure TForm1.ShowResult(fromval,toval : Double; FromType,ToType :TConvType);
  48. begin
  49.    ResultEdit.Text := Format('%g %s = %g %s',
  50.                         [fromval,ConvTypeToDescription(FromType),
  51.                          toval,ConvTypeToDescription(ToType)]);
  52. end;
  53.  
  54. procedure TForm1.FormCreate(Sender: TObject);
  55. var
  56.   FamilyList: TConvFamilyArray;
  57.   i: Integer;
  58. begin
  59.   GetConvFamilies(FamilyList);
  60.   for i := 0 to Length(FamilyList)- 1 do
  61.     ComboBox1.Items.Add(ConvFamilyToDescription(FamilyList[i]));
  62.   ComboBox1.ItemIndex := 0; { select the first item }
  63.   ComboBox1Select(ComboBox1); {trigger the event to initialize lists}
  64. end;
  65.  
  66. procedure TForm1.ComboBox1Select(Sender: TObject);
  67. var
  68.   TypeList: TConvTypeArray;
  69.   i: Integer;
  70.   CurFamily:TConvFamily;
  71.   Description: string;
  72. begin
  73.   Description := ComboBox1.Items[ComboBox1.ItemIndex];
  74.   if DescriptionToConvFamily(Description, CurFamily) then
  75.   begin
  76.     GetConvTypes(CurFamily, TypeList);
  77.     ListBox1.Items.Clear; // we've added this
  78.     for i := 0 to Length(TypeList) -1 do
  79.       ListBox1.Items.Add(ConvTypeToDescription(TypeList[i]));
  80.     ListBox2.Items := ListBox1.Items;
  81.     ListBox1.ItemIndex := 0;
  82.     ListBox2.ItemIndex := 0;
  83.   end;
  84. end;
  85.  
  86. procedure TForm1.Button1Click(Sender: TObject);
  87. var
  88.   dblval,newval: Double;
  89.   CurFamily: TConvFamily;
  90.   FromType, ToType: TConvType;
  91. begin
  92.   ShowMsg('');
  93.   dblval := StrToFloatDef(Edit1.Text, 0.0);
  94.   if dblval = 0.0 then
  95.      ShowMsg('Enter a meaningful integer or floating point value.' )
  96.   else
  97.   begin
  98.     DescriptionToConvFamily(ComboBox1.Items[ComboBox1.ItemIndex], CurFamily);
  99.     DescriptionToConvType(CurFamily, ListBox1.Items[ListBox1.ItemIndex], FromType);
  100.     DescriptionToConvType(CurFamily, ListBox2.Items[ListBox2.ItemIndex], ToType);
  101.  // the following is incorrect in the Help file example
  102.  // newVal := Convert(FromType, ToType, StrToFloat(Edit1.Text));
  103.     newVal := Convert(dblVal, FromType, ToType );
  104.  // we've added our own result display
  105.  //    ShowMessage(Format('%g %s', [newVal, ConvTypeToDescription(ToType)]));
  106.     ShowResult(dblval,newval,FromType,ToType);
  107.   end;
  108. end;
  109.  
  110. end.
  111.