回答編集履歴
2
コメント追加
answer
CHANGED
@@ -226,10 +226,10 @@
|
|
226
226
|
|
227
227
|
private void FormC_FormClosing(object sender, FormClosingEventArgs e)
|
228
228
|
{
|
229
|
-
if (e.CloseReason == CloseReason.UserClosing)
|
229
|
+
if (e.CloseReason == CloseReason.UserClosing) // ユーザーが閉じようとしたら...
|
230
230
|
{
|
231
|
-
e.Cancel = true;
|
231
|
+
e.Cancel = true; // 閉じるのを中止して
|
232
|
-
Hide();
|
232
|
+
Hide(); // 非表示にする
|
233
233
|
}
|
234
234
|
}
|
235
235
|
}
|
1
単一化サンプル追記
answer
CHANGED
@@ -104,4 +104,135 @@
|
|
104
104
|
そのため「スタートボタン」を押すたび、1ずつ・2ずつ・3ずつ...とカウントアップ数が増えていきます。
|
105
105
|
|
106
106
|
こういった方法で都度newしなければ、そのあたりを気にしないで済むかもしれません。
|
107
|
-
[フォームの×ボタンで閉じないで、Hide にする。 - SN工房](http://snfactory.blog54.fc2.com/blog-entry-30.html)
|
107
|
+
[フォームの×ボタンで閉じないで、Hide にする。 - SN工房](http://snfactory.blog54.fc2.com/blog-entry-30.html)
|
108
|
+
|
109
|
+
## 追記
|
110
|
+
|
111
|
+
```cs:FormA.cs
|
112
|
+
using System;
|
113
|
+
using System.Windows.Forms;
|
114
|
+
|
115
|
+
|
116
|
+
namespace Qcqonfgr3inq2r5
|
117
|
+
{
|
118
|
+
public partial class FormA : Form
|
119
|
+
{
|
120
|
+
public FormA()
|
121
|
+
{
|
122
|
+
InitializeComponent();
|
123
|
+
|
124
|
+
// FormCは閉じずにHide作戦なので、newは1回だけ
|
125
|
+
fmC = new FormC(this);
|
126
|
+
timer2.Tick += fmC.Timer_Tick;
|
127
|
+
}
|
128
|
+
|
129
|
+
#region FormB
|
130
|
+
private readonly Timer timer1 = new Timer { Interval = 500, };
|
131
|
+
private FormB fmB; // 最初はnull
|
132
|
+
private int value1;
|
133
|
+
|
134
|
+
public void Value1() => value1++;
|
135
|
+
public void ResultOutput1() => textBox1.Text = value1.ToString();
|
136
|
+
|
137
|
+
private void ButtonStart1(object sender, EventArgs e)
|
138
|
+
{
|
139
|
+
// もしFormBがすでにnewされていたら、イベント解除&閉じる
|
140
|
+
if (fmB != null)
|
141
|
+
{
|
142
|
+
timer1.Tick -= fmB.Timer_Tick;
|
143
|
+
fmB.Close();
|
144
|
+
}
|
145
|
+
|
146
|
+
// FormBを作ってイベント登録&表示
|
147
|
+
fmB = new FormB(this);
|
148
|
+
timer1.Tick += fmB.Timer_Tick;
|
149
|
+
fmB.Show();
|
150
|
+
|
151
|
+
timer1.Start();
|
152
|
+
}
|
153
|
+
private void ButtonStop1(object sender, EventArgs e) => timer1.Stop();
|
154
|
+
#endregion
|
155
|
+
|
156
|
+
|
157
|
+
#region FormC
|
158
|
+
private readonly Timer timer2 = new Timer { Interval = 500, };
|
159
|
+
private readonly FormC fmC;
|
160
|
+
private int value2;
|
161
|
+
|
162
|
+
public void Value2() => value2++;
|
163
|
+
public void ResultOutput2() => textBox2.Text = value2.ToString();
|
164
|
+
|
165
|
+
private void ButtonStart2(object sender, EventArgs e)
|
166
|
+
{
|
167
|
+
fmC.Show();
|
168
|
+
fmC.Activate(); // ほかのウィンドウに隠れていたら気が付かないのでアクティブ化
|
169
|
+
fmC.WindowState = FormWindowState.Normal; // アイコン化してたら復帰
|
170
|
+
|
171
|
+
timer2.Start();
|
172
|
+
}
|
173
|
+
private void ButtonStop2(object sender, EventArgs e) => timer2.Stop();
|
174
|
+
#endregion
|
175
|
+
}
|
176
|
+
}
|
177
|
+
```
|
178
|
+
```cs:FormB.cs
|
179
|
+
using System;
|
180
|
+
using System.Windows.Forms;
|
181
|
+
|
182
|
+
|
183
|
+
namespace Qcqonfgr3inq2r5
|
184
|
+
{
|
185
|
+
public partial class FormB : Form
|
186
|
+
{
|
187
|
+
private readonly FormA fmA;
|
188
|
+
|
189
|
+
public FormB(FormA formA)
|
190
|
+
{
|
191
|
+
fmA = formA;
|
192
|
+
InitializeComponent();
|
193
|
+
}
|
194
|
+
|
195
|
+
public void Timer_Tick(object sender, EventArgs e)
|
196
|
+
{
|
197
|
+
fmA.Value1();
|
198
|
+
fmA.ResultOutput1();
|
199
|
+
}
|
200
|
+
}
|
201
|
+
}
|
202
|
+
```
|
203
|
+
```cs:FormC.cs
|
204
|
+
using System;
|
205
|
+
using System.Windows.Forms;
|
206
|
+
|
207
|
+
|
208
|
+
namespace Qcqonfgr3inq2r5
|
209
|
+
{
|
210
|
+
public partial class FormC : Form
|
211
|
+
{
|
212
|
+
private readonly FormA fmA;
|
213
|
+
|
214
|
+
public FormC(FormA formA)
|
215
|
+
{
|
216
|
+
fmA = formA;
|
217
|
+
InitializeComponent();
|
218
|
+
FormClosing += FormC_FormClosing;
|
219
|
+
}
|
220
|
+
|
221
|
+
public void Timer_Tick(object sender, EventArgs e)
|
222
|
+
{
|
223
|
+
fmA.Value2();
|
224
|
+
fmA.ResultOutput2();
|
225
|
+
}
|
226
|
+
|
227
|
+
private void FormC_FormClosing(object sender, FormClosingEventArgs e)
|
228
|
+
{
|
229
|
+
if (e.CloseReason == CloseReason.UserClosing)
|
230
|
+
{
|
231
|
+
e.Cancel = true;
|
232
|
+
Hide();
|
233
|
+
}
|
234
|
+
}
|
235
|
+
}
|
236
|
+
}
|
237
|
+
```
|
238
|
+

|