Programming/Delphi

방향키로 에디트박스 포커스 이동하기

통통만두 2011. 1. 21. 14:58
반응형
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Edit7: TEdit;
    Edit8: TEdit;
    Edit9: TEdit;
    Edit10: TEdit;
    Edit11: TEdit;
    Edit12: TEdit;
    Edit13: TEdit;
    Edit14: TEdit;
    Edit15: TEdit;
    Edit16: TEdit;
    Edit17: TEdit;
    Edit18: TEdit;
    Edit19: TEdit;
    Edit20: TEdit;
    Edit21: TEdit;
    Edit22: TEdit;
    Edit23: TEdit;
    Edit24: TEdit;
    Edit25: TEdit;
    Edit26: TEdit;
    Edit27: TEdit;
    Edit28: TEdit;
    Edit29: TEdit;
    Edit30: TEdit;
    Edit31: TEdit;
    Edit32: TEdit;
    Edit33: TEdit;
    Edit34: TEdit;
    Edit35: TEdit;
    Edit36: TEdit;
    Edit37: TEdit;
    Edit38: TEdit;
    Edit39: TEdit;
    Edit40: TEdit;
    Edit41: TEdit;
    Edit42: TEdit;
    Edit43: TEdit;
    Edit44: TEdit;
    Edit45: TEdit;
    Edit46: TEdit;
    Edit47: TEdit;
    Edit48: TEdit;
    Edit49: TEdit;
    Edit50: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure Edit1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
    EditPos : array[1..5, 1..10] of Integer;
    EditObj : array[1..5, 1..10] of TEdit;
    procedure InitEditPos(const x, y: Integer);
    function CanMove(const x, y: Integer; Key: Word): Boolean;
    procedure CurrentPos(var x, y: Integer);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  Buffer : String;
  i, j : Integer;
begin
  InitEditPos(1, 1);

  for i := 1 to 5 do
  begin
    for j := 1 to 10 do
    begin
      if i = 1 then Buffer := IntToStr(j)
      else
      begin
        if j <> 10 then Buffer := Format('%d%d', [i-1, j])
        else            Buffer := Format('%d%d', [i, 0]);
      end;

      EditObj[i, j] := TEdit(FindComponent('Edit' + Buffer));

      TEdit(EditObj[i, j]).Text := '' + Buffer;
    end;
  end;
end;

procedure TForm1.InitEditPos(const x, y: Integer);
var
  i, j : Integer;
begin
  for i := 1 to 5 do
  begin
    for j := 1 to 10 do
    begin
      EditPos[i, j] := 0;
    end;
  end;
  EditPos[x, y] := 1;
end;

function TForm1.CanMove(const x, y: Integer; Key: Word) : Boolean;
begin
  case Key of
    VK_LEFT  : Result := x <> 1;
    VK_RIGHT : Result := x <> 5;
    VK_UP    : Result := y <> 1;
    VK_DOWN  : Result := y <> 10;
  end;
end;

procedure TForm1.CurrentPos(var x, y: Integer);
var
  i, j : Integer;
begin
  for i := 1 to 5 do
  begin
    for j := 1 to 10 do
    begin
      if EditPos[i, j] = 1 then
      begin
        x := i;
        y := j;
        Break;
      end;
    end;
  end;
end;

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  x, y: Integer;
begin
  CurrentPos(x, y);

  case Key of
    VK_LEFT  :
    begin
      if CanMove(x, y, Key) then
      begin
        InitEditPos(x-1, y);
        TEdit(EditObj[x-1, y]).SetFocus;
      end;
    end;

    VK_RIGHT :
    begin
      if CanMove(x, y, Key) then
      begin
        InitEditPos(x+1, y);
        TEdit(EditObj[x+1, y]).SetFocus;
      end;
    end;

    VK_UP    :
    begin
      if CanMove(x, y, Key) then
      begin
        InitEditPos(x, y-1);
        TEdit(EditObj[x, y-1]).SetFocus;
      end;
    end;

    VK_DOWN  :
    begin
      if CanMove(x, y, Key) then
      begin
        InitEditPos(x, y+1);
        TEdit(EditObj[x, y+1]).SetFocus;
      end;
    end;
  end;
end;

end.




반응형