回答編集履歴

1

2022/02/07 18:55

投稿

KOZ6.0
KOZ6.0

スコア2628

test CHANGED
@@ -3,6 +3,8 @@
3
3
 
4
4
  トリッキーな方法だと、ProcessTabKey をオーバーライドすること。
5
5
  リストを作って、次のように登録順に動かすことができます。
6
+
7
+ ikedayuuki さんの[前の質問](https://teratail.com/questions/sj4q31iduw3haw)を加味すると
6
8
 
7
9
  ```C#
8
10
  using System.Collections.Generic;
@@ -19,7 +21,28 @@
19
21
  }
20
22
 
21
23
  protected override bool ProcessTabKey(bool forward) {
24
+ FocusedControl = GetNextControl(FocusedControl, forward);
25
+ return true;
26
+ }
27
+
28
+ private Control FocusedControl {
29
+ get {
30
+ ContainerControl container = this;
31
+ while (container.ActiveControl is ContainerControl control) {
32
+ container = control;
33
+ }
34
+ return container.ActiveControl ?? container;
35
+ }
36
+ set {
37
+ ActiveControl = value;
38
+ if (value is ContainerControl container) {
39
+ container.ActiveControl = null;
40
+ }
41
+ }
42
+ }
43
+
44
+ public Control GetNextTabOrder(Control ctl, bool forward) {
22
- int index = tabOrderList.IndexOf(FocusedControl);
45
+ int index = tabOrderList.IndexOf(ctl);
23
46
  if (index >= 0) {
24
47
  index += forward ? 1 : -1;
25
48
  if (index < 0) {
@@ -27,28 +50,36 @@
27
50
  } else if (index >= tabOrderList.Count) {
28
51
  index = 0;
29
52
  }
30
- FocusedControl = tabOrderList[index];
53
+ return tabOrderList[index];
54
+ } else {
31
- return true;
55
+ return ctl;
32
56
  }
33
- return base.ProcessTabKey(forward);
34
57
  }
35
58
 
59
+ public new Control GetNextControl(Control ctl, bool forward) {
60
+ Control next = GetNextTabOrder(ctl, forward);
61
+ while (next != ctl) {
62
+ if (next.CanSelect && next.TabStop) {
63
+ return next;
64
+ }
65
+ next = GetNextTabOrder(next, forward);
66
+ }
67
+ return ctl;
68
+ }
69
+
36
- private Control FocusedControl {
70
+ private TextBox NextTextBox {
37
71
  get {
38
- ContainerControl container = this;
72
+ Control first = FocusedControl;
39
- while (container.ActiveControl is ContainerControl) {
40
- container = (ContainerControl)container.ActiveControl;
73
+ Control next = GetNextTabOrder(first, true);
74
+ while (!(next is TextBox)) {
75
+ next = GetNextTabOrder(next, true);
76
+ if (next == first) {
77
+ return null; // 一周しても TextBox が存在しなかった
78
+ }
41
79
  }
42
- return container.ActiveControl ?? container;
43
- }
44
- set {
45
- ActiveControl = value;
46
- ContainerControl container = value as ContainerControl;
47
- if (container != null) {
48
- container.ActiveControl = null;
80
+ return (TextBox)next;
49
- }
50
81
  }
51
82
  }
52
83
  }
53
84
  ```
54
-
85
+ こんな感じになると思います。