Свой обработчик переключения между элементами на форме

03.04.2013
Для того чтобы реализовать переход от (передачу фокуса) одного valuelisteditor к другому нужно было отследить  нажатие кнопок tab и enter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
unit uTest;

interface

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

type
  TForm1 = class(TForm)
    ValueListEditor1: TValueListEditor;
    ValueListEditor2: TValueListEditor;
    procedure Button1Click(Sender: TObject);
  private
    procedure CMDialogKey( var msg: TCMDialogKey );
    message CM_DIALOGKEY;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.CMDialogKey(var msg: TCMDialogKey);
var
  iActRow : integer;
begin
  if msg.Charcode = VK_TAB then
    begin
      Form1.Caption := 'Tab Key Down!';
      iActRow:= ValueListEditor1.Row;
      inc(iActRow);
      if iActRow >= ValueListEditor1.RowCount then
        begin
          ValueListEditor2.Row:= 1;
          ActiveControl:= nil;
          ActiveControl:= ValueListEditor2;
        end
      else
        begin
          ValueListEditor1.Row:=iActRow;
        end
      ;

      iActRow:= ValueListEditor2.Row;
      inc(iActRow);
      if iActRow < ValueListEditor2.RowCount then
      begin
        ValueListEditor2.Row:=iActRow;
      end;

    end
  else
    begin
      inherited;
    end
  ;
end;

end.

Доделанный вариант (еще обрабатывается нажатие enter);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
unit uTest;

interface

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

type
  TForm1 = class(TForm)
    ValueListEditor1: TValueListEditor;
    ValueListEditor2: TValueListEditor;
    procedure FormCreate(Sender: TObject);
    procedure ValueListEditor1KeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure ValueListEditor2KeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
    procedure CMDialogKey( var msg: TCMDialogKey );
    message CM_DIALOGKEY;

    procedure prcGoNext();
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.CMDialogKey(var msg: TCMDialogKey);
begin
  if msg.Charcode = VK_TAB then
    begin
      prcGoNext();
    end
  else
    begin
      inherited;
    end
  ;
end;

procedure TForm1.prcGoNext;
var
  iActRow : integer;
begin

  if ActiveControl = ValueListEditor1 then
    begin
      iActRow:= ValueListEditor1.Row;
      inc(iActRow);
      if iActRow >= ValueListEditor1.RowCount then
        begin
          ValueListEditor2.Row:= 1;
          ActiveControl:= nil;
          ActiveControl:= ValueListEditor2;
        end
      else
        begin
          ValueListEditor1.Row:=iActRow;
        end
      ;
    end
  else
    begin
      if ActiveControl = ValueListEditor2 then
      begin
        iActRow:= ValueListEditor2.Row;
        inc(iActRow);
        if iActRow < ValueListEditor2.RowCount then
        begin
          ValueListEditor2.Row:=iActRow;
        end;
      end;
    end
  ;

end;

procedure TForm1.FormCreate(Sender: TObject);
var
  i :Integer;
begin
  for i := 1 to 8 do
  begin
    ValueListEditor1.Strings.Values[IntToStr(i)]:=' ';
  end;

  for i := 8 to 12 do
  begin
    ValueListEditor2.Strings.Values[IntToStr(i)]:=' ';
  end;

end;

procedure TForm1.ValueListEditor1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_RETURN then
    begin
      prcGoNext();
    end
  else
    begin
      inherited;
    end
  ;
end;

procedure TForm1.ValueListEditor2KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_RETURN then
    begin
      prcGoNext();
    end
  else
    begin
      inherited;
    end
  ;
end;

end.


Категории: Delphi
Яндекс.Метрика