Subversion Repositories currency_converter

Rev

Blame | Last modification | View Log | RSS feed

  1. unit Demo;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, StdCtrls, ComCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     ComboBox1: TComboBox;
  12.     ComboBox2: TComboBox;
  13.     Edit1: TEdit;
  14.     Edit2: TEdit;
  15.     Label1: TLabel;
  16.     DateTimePicker1: TDateTimePicker;
  17.     CheckBox1: TCheckBox;
  18.     procedure FormShow(Sender: TObject);
  19.     procedure Recalc(Sender: TObject);
  20.     procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  21.   private
  22.     Initialized: boolean;
  23.     procedure FillComboboxes;
  24.     function HistoricDate: TDate;
  25.   end;
  26.  
  27. var
  28.   Form1: TForm1;
  29.  
  30. implementation
  31.  
  32. {$R *.dfm}
  33.  
  34. uses
  35.   Math, VtsCurConvDLLHeader;
  36.  
  37. const
  38.   MaxAge = 1*60*60;
  39.   Flags = CONVERT_FALLBACK_TO_CACHE;
  40.  
  41. procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  42. var
  43.   tmp: integer;
  44. begin
  45.   if Key = VK_F5 then
  46.   begin
  47.     tmp := ComboBox1.ItemIndex;
  48.     ComboBox1.ItemIndex := ComboBox2.ItemIndex;
  49.     ComboBox2.ItemIndex := tmp;
  50.     Recalc(Sender);
  51.   end;
  52. end;
  53.  
  54. procedure TForm1.FormShow(Sender: TObject);
  55. begin
  56.   FillComboboxes;
  57.   ComboBox1.ItemIndex := Max(0, ComboBox1.Items.IndexOf('USD'));
  58.   ComboBox2.ItemIndex := Max(0, ComboBox2.Items.IndexOf('EUR'));
  59.   Edit1.Text := '';
  60.   Edit2.Text := '';
  61.   Edit1.SetFocus;
  62.   Edit1.Text := '1';
  63.   Edit1.SelectAll;
  64.  
  65.   Initialized := true;
  66.   Recalc(Sender);
  67. end;
  68.  
  69. function TForm1.HistoricDate: TDate;
  70. begin
  71.   if CheckBox1.Checked then
  72.     result := DateTimePicker1.Date
  73.   else
  74.     result := 0;
  75. end;
  76.  
  77. procedure TForm1.Recalc(Sender: TObject);
  78. var
  79.   s: string;
  80.   d: double;
  81. begin
  82.   if not Initialized then exit;  
  83.   s := Edit1.Text;
  84.   if TryStrToFloat(s, d) and (ComboBox1.Text <> '') and (ComboBox2.Text <> '') then
  85.     Edit2.Text := Format('%.2f', [Convert(d, PChar(ComboBox1.Text), PChar(ComboBox2.Text), MaxAge, Flags, HistoricDate)])
  86.   else
  87.     Edit2.Text := '';
  88. end;
  89.  
  90. procedure TForm1.FillComboboxes;
  91. var
  92.   num: integer;
  93.   s: string;
  94.   i: integer;
  95. begin
  96.   num := AcceptedCurrencies(nil, MaxAge, Flags, HistoricDate);
  97.   SetLength(s, 3*num+1);
  98.   num := AcceptedCurrencies(PChar(s), MaxAge, Flags, HistoricDate);
  99.   ComboBox1.Clear;
  100.   ComboBox2.Clear;
  101.   for i := 0 to num - 1 do
  102.   begin
  103.     ComboBox1.Items.Add(Copy(s, i*3+1, 3));
  104.     ComboBox2.Items.Add(Copy(s, i*3+1, 3));
  105.   end;
  106. end;
  107.  
  108. end.
  109.