Please modify To Delphi:  
Code
unit pd1; 
 interface 
 uses 
   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
   Dialogs, StdCtrls, Grids; 
 type 
   TForm1 = class(TForm) 
     ListBox2: TListBox; 
     StringGrid1: TStringGrid; 
     ListBox1: TListBox; 
     Button1: TButton; 
     Button2: TButton; 
     Label1: TLabel; 
     Button3: TButton; 
     Button4: TButton; 
     Edit1: TEdit; 
     procedure ListBox2KeyDown(Sender: TObject; var Key: Word; 
       Shift: TShiftState); 
     procedure Button3Click(Sender: TObject); 
     procedure Button4Click(Sender: TObject); 
     procedure Button1Click(Sender: TObject); 
   private 
     { Private declarations } 
   public 
     { Public declarations } 
   end; 
 var 
   Form1: TForm1; 
 implementation 
 {$R *.dfm} 
 procedure TForm1.ListBox2KeyDown(Sender: TObject; var Key: Word; 
   Shift: TShiftState); 
 begin 
 Caption:=inttostr(key); 
 end; 
 procedure TForm1.Button3Click(Sender: TObject); 
 begin 
 try 
 ListBox2.items.Add(inttostr(strtoint(Edit1.Text))); 
 Edit1.Text:=''; 
 except 
 beep; 
 showmessage('Nekorekts ievads!'); 
 end; 
 Edit1.SetFocus; 
 end; 
 procedure TForm1.Button4Click(Sender: TObject); 
 begin 
 ListBox2.Items.Clear; 
 end; 
 procedure TForm1.Button1Click(Sender: TObject); 
 var trigger:boolean;i:integer; 
 begin 
 trigger:=false; 
 if sender=button1 then 
  ListBox1.Items.Clear 
 else 
  begin 
   StringGrid1.Cells[0,0]:=''; 
   StringGrid1.RowCount:=1; 
  end; 
 for i:=0 to ListBox2.Items.Count-2 do 
 if strtoint(ListBox2.Items[i])>strtoint(ListBox2.Items[i+1]) 
  then 
   if not trigger then 
    begin 
     trigger:=true; 
     if sender=button1 then 
      ListBox1.Items.add(inttostr(i)) 
     else 
      begin 
       if StringGrid1.Cells[0,StringGrid1.RowCount-1]<>'' then 
        StringGrid1.RowCount:=StringGrid1.RowCount+1; 
      StringGrid1.Cells[0,StringGrid1.RowCount-1]:=inttostr(i); 
     end 
    end 
   else 
  else 
   trigger:=false; 
 Edit1.SetFocus; 
 end; 
 end.