回答編集履歴
1
見直しキャンペーン中
answer
CHANGED
@@ -1,52 +1,52 @@
|
|
1
|
-
[TabControl.Deselecting イベント (System.Windows.Forms) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.forms.tabcontrol.deselecting)
|
2
|
-
がいいんじゃないでしょうか。
|
3
|
-
|
4
|
-
```
|
5
|
-
using System;
|
6
|
-
using System.Drawing;
|
7
|
-
using System.Windows.Forms;
|
8
|
-
|
9
|
-
namespace Questions289215
|
10
|
-
{
|
11
|
-
public partial class Form1 : Form
|
12
|
-
{
|
13
|
-
public Form1()
|
14
|
-
{
|
15
|
-
InitializeComponent();
|
16
|
-
|
17
|
-
var tabControl = new TabControl { Dock = DockStyle.Fill, };
|
18
|
-
tabControl.Deselecting += TabControl_Deselecting;
|
19
|
-
Controls.Add(tabControl);
|
20
|
-
|
21
|
-
for(var i = 1; i < 11; i++)
|
22
|
-
{
|
23
|
-
var tabPage = new TabPage($"Page{i}");
|
24
|
-
tabControl.TabPages.Add(tabPage);
|
25
|
-
|
26
|
-
tabPage.Controls.Add(new TextBox { Name = "textBox", });
|
27
|
-
var button = new Button() { Text = "OK", Location = new Point(0, 30), };
|
28
|
-
button.Click += Button_Click;
|
29
|
-
tabPage.Controls.Add(button);
|
30
|
-
}
|
31
|
-
}
|
32
|
-
|
33
|
-
private void TabControl_Deselecting(object sender, TabControlCancelEventArgs e)
|
34
|
-
{
|
35
|
-
if(e.TabPage.Tag == null) // OK押してなければ。。。
|
36
|
-
{
|
37
|
-
MessageBox.Show("OKボタンを押下していません");
|
38
|
-
//e.Cancel = true; // ページ移動をキャンセルするなら
|
39
|
-
}
|
40
|
-
}
|
41
|
-
|
42
|
-
private void Button_Click(object sender, EventArgs e)
|
43
|
-
{
|
44
|
-
if(sender is Button button)
|
45
|
-
{
|
46
|
-
var textBox = button.Parent.Controls["textBox"] as TextBox;
|
47
|
-
button.Parent.Tag = "OK"; // OK押したフラグ(Tagを使ったのには深い意味はない
|
48
|
-
}
|
49
|
-
}
|
50
|
-
}
|
51
|
-
}
|
1
|
+
[TabControl.Deselecting イベント (System.Windows.Forms) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.forms.tabcontrol.deselecting)
|
2
|
+
がいいんじゃないでしょうか。
|
3
|
+
|
4
|
+
```cs
|
5
|
+
using System;
|
6
|
+
using System.Drawing;
|
7
|
+
using System.Windows.Forms;
|
8
|
+
|
9
|
+
namespace Questions289215
|
10
|
+
{
|
11
|
+
public partial class Form1 : Form
|
12
|
+
{
|
13
|
+
public Form1()
|
14
|
+
{
|
15
|
+
InitializeComponent();
|
16
|
+
|
17
|
+
var tabControl = new TabControl { Dock = DockStyle.Fill, };
|
18
|
+
tabControl.Deselecting += TabControl_Deselecting;
|
19
|
+
Controls.Add(tabControl);
|
20
|
+
|
21
|
+
for(var i = 1; i < 11; i++)
|
22
|
+
{
|
23
|
+
var tabPage = new TabPage($"Page{i}");
|
24
|
+
tabControl.TabPages.Add(tabPage);
|
25
|
+
|
26
|
+
tabPage.Controls.Add(new TextBox { Name = "textBox", });
|
27
|
+
var button = new Button() { Text = "OK", Location = new Point(0, 30), };
|
28
|
+
button.Click += Button_Click;
|
29
|
+
tabPage.Controls.Add(button);
|
30
|
+
}
|
31
|
+
}
|
32
|
+
|
33
|
+
private void TabControl_Deselecting(object sender, TabControlCancelEventArgs e)
|
34
|
+
{
|
35
|
+
if(e.TabPage.Tag == null) // OK押してなければ。。。
|
36
|
+
{
|
37
|
+
MessageBox.Show("OKボタンを押下していません");
|
38
|
+
//e.Cancel = true; // ページ移動をキャンセルするなら
|
39
|
+
}
|
40
|
+
}
|
41
|
+
|
42
|
+
private void Button_Click(object sender, EventArgs e)
|
43
|
+
{
|
44
|
+
if(sender is Button button)
|
45
|
+
{
|
46
|
+
var textBox = button.Parent.Controls["textBox"] as TextBox;
|
47
|
+
button.Parent.Tag = "OK"; // OK押したフラグ(Tagを使ったのには深い意味はない
|
48
|
+
}
|
49
|
+
}
|
50
|
+
}
|
51
|
+
}
|
52
52
|
```
|