unit FormColumns;

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, VCard, FormEx;

type

  { TFormColumns }

  TFormColumns = class(TFormEx)
    ButtonUp: TButton;
    ButtonToLeft: TButton;
    ButtonCancel: TButton;
    ButtonOk: TButton;
    ButtonToRight: TButton;
    ButtonDown: TButton;
    Label1: TLabel;
    Label2: TLabel;
    ListBoxActive: TListBox;
    ListBoxAvailable: TListBox;
    procedure ButtonDownClick(Sender: TObject);
    procedure ButtonOkClick(Sender: TObject);
    procedure ButtonToLeftClick(Sender: TObject);
    procedure ButtonToRightClick(Sender: TObject);
    procedure ButtonUpClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ListBoxActiveSelectionChange(Sender: TObject; User: boolean);
    procedure ListBoxAvailableSelectionChange(Sender: TObject; User: boolean);
  private
    procedure SwapListItems(ListBox: TListBox; Index1, Index2: Integer);
  public
    ActiveColumns: TStringList;
    AvailableColumns: TStringList;
    procedure UpdateInterface;
  end;


implementation

{$R *.lfm}

{ TFormColumns }

procedure TFormColumns.FormShow(Sender: TObject);
begin
  ListBoxActive.Items.Assign(ActiveColumns);
  ListBoxAvailable.Items.Assign(AvailableColumns);
  ListBoxAvailable.Sorted := True;

  UpdateInterface;
end;

procedure TFormColumns.ListBoxActiveSelectionChange(Sender: TObject;
  User: boolean);
begin
  UpdateInterface;
end;

procedure TFormColumns.ListBoxAvailableSelectionChange(Sender: TObject;
  User: boolean);
begin
  UpdateInterface;
end;

procedure TFormColumns.SwapListItems(ListBox: TListBox; Index1, Index2: Integer
  );
var
  Temp: string;
  TempObject: TObject;
begin
  Temp := ListBox.Items[Index1];
  ListBox.Items[Index1] := ListBox.Items[Index2];
  ListBox.Items[Index2] := Temp;
  TempObject := ListBox.Items.Objects[Index1];
  ListBox.Items.Objects[Index1] := ListBox.Items.Objects[Index2];
  ListBox.Items.Objects[Index2] := TempObject;
end;

procedure TFormColumns.ButtonUpClick(Sender: TObject);
begin
  if ListBoxActive.ItemIndex > 0 then begin
    SwapListItems(ListBoxActive, ListBoxActive.ItemIndex, ListBoxActive.ItemIndex - 1);
    ListBoxActive.ItemIndex := ListBoxActive.ItemIndex - 1;
  end;
end;

procedure TFormColumns.ButtonDownClick(Sender: TObject);
begin
  if ListBoxActive.ItemIndex < ListBoxActive.Items.Count - 1 then begin
    SwapListItems(ListBoxActive, ListBoxActive.ItemIndex, ListBoxActive.ItemIndex + 1);
    ListBoxActive.ItemIndex := ListBoxActive.ItemIndex + 1;
  end;
end;

procedure TFormColumns.ButtonOkClick(Sender: TObject);
begin
  ActiveColumns.Assign(ListBoxActive.Items);
end;

procedure TFormColumns.ButtonToLeftClick(Sender: TObject);
begin
  if ListBoxActive.ItemIndex = -1 then begin
   ListBoxActive.Items.AddObject(ListBoxAvailable.Items[ListBoxAvailable.ItemIndex],
      ListBoxAvailable.Items.Objects[ListBoxAvailable.ItemIndex])
  end else begin
    ListBoxActive.Items.InsertObject(ListBoxActive.ItemIndex,
      ListBoxAvailable.Items[ListBoxAvailable.ItemIndex],
      ListBoxAvailable.Items.Objects[ListBoxAvailable.ItemIndex]);
  end;
  ListBoxAvailable.Items.Delete(ListBoxAvailable.ItemIndex);
end;

procedure TFormColumns.ButtonToRightClick(Sender: TObject);
begin
  ListBoxAvailable.Items.AddObject(ListBoxActive.Items[ListBoxActive.ItemIndex],
    ListBoxActive.Items.Objects[ListBoxActive.ItemIndex]);
  ListBoxActive.Items.Delete(ListBoxActive.ItemIndex);
end;

procedure TFormColumns.FormCreate(Sender: TObject);
begin
  ActiveColumns := TStringList.Create;
  AvailableColumns := TStringList.Create;
end;

procedure TFormColumns.FormDestroy(Sender: TObject);
begin
  FreeAndNil(ActiveColumns);
  FreeAndNil(AvailableColumns);
end;

procedure TFormColumns.UpdateInterface;
begin
  ButtonToLeft.Enabled := ListBoxAvailable.ItemIndex <> -1;
  ButtonToRight.Enabled := ListBoxActive.ItemIndex <> -1;
  ButtonDown.Enabled := (ListBoxActive.ItemIndex <> -1) and (ListBoxActive.ItemIndex < ListBoxActive.Items.Count - 1);
  ButtonUp.Enabled := (ListBoxActive.ItemIndex <> -1) and (ListBoxActive.ItemIndex > 0);
end;

end.

