回答編集履歴

2

AutoApplicationContext を改善

2021/11/13 17:42

投稿

KOZ6.0
KOZ6.0

スコア2707

test CHANGED
@@ -118,17 +118,11 @@
118
118
 
119
119
  {
120
120
 
121
- public AutoApplicationContext(Form form) : base(form)
121
+ public AutoApplicationContext(Form form) : base(form) { }
122
-
123
- {
124
-
125
- form.FormClosed += Form_FormClosed;
126
-
127
- }
128
122
 
129
123
 
130
124
 
131
- private void Form_FormClosed(object sender, FormClosedEventArgs e)
125
+ protected override void OnMainFormClosed(object sender, EventArgs e)
132
126
 
133
127
  {
134
128
 
@@ -142,13 +136,13 @@
142
136
 
143
137
  this.MainForm = form;
144
138
 
145
- form.FormClosed += Form_FormClosed;
146
-
147
- break;
139
+ return;
148
140
 
149
141
  }
150
142
 
151
143
  }
144
+
145
+ base.OnMainFormClosed(sender, e);
152
146
 
153
147
  }
154
148
 

1

VB6 風にしてみた

2021/11/13 17:42

投稿

KOZ6.0
KOZ6.0

スコア2707

test CHANGED
@@ -71,3 +71,119 @@
71
71
  }
72
72
 
73
73
  ```
74
+
75
+ #追記
76
+
77
+
78
+
79
+ VB6 風に、フォームが残っていたらメインフォームを自動で切り替えるようにしてみました。
80
+
81
+ (VB.NET の「Windowsアプリケーションフレームワークプロパティ」の
82
+
83
+ 「シャットダウンモード」を「最後のフォームが閉じるとき」にしたときの動き)
84
+
85
+
86
+
87
+ ```C#
88
+
89
+ using System;
90
+
91
+ using System.Windows.Forms;
92
+
93
+
94
+
95
+ static class Program
96
+
97
+ {
98
+
99
+ [STAThread]
100
+
101
+ static void Main()
102
+
103
+ {
104
+
105
+ Application.EnableVisualStyles();
106
+
107
+ Application.SetCompatibleTextRenderingDefault(false);
108
+
109
+ Application.Run(new AutoApplicationContext(new Form1()));
110
+
111
+ }
112
+
113
+ }
114
+
115
+
116
+
117
+ class AutoApplicationContext : ApplicationContext
118
+
119
+ {
120
+
121
+ public AutoApplicationContext(Form form) : base(form)
122
+
123
+ {
124
+
125
+ form.FormClosed += Form_FormClosed;
126
+
127
+ }
128
+
129
+
130
+
131
+ private void Form_FormClosed(object sender, FormClosedEventArgs e)
132
+
133
+ {
134
+
135
+ foreach (Form form in Application.OpenForms)
136
+
137
+ {
138
+
139
+ if (!form.InvokeRequired)
140
+
141
+ {
142
+
143
+ this.MainForm = form;
144
+
145
+ form.FormClosed += Form_FormClosed;
146
+
147
+ break;
148
+
149
+ }
150
+
151
+ }
152
+
153
+ }
154
+
155
+ }
156
+
157
+
158
+
159
+ public partial class Form1 : Form
160
+
161
+ {
162
+
163
+ public Form1()
164
+
165
+ {
166
+
167
+ InitializeComponent();
168
+
169
+ }
170
+
171
+
172
+
173
+ private void button1_Click(object sender, EventArgs e)
174
+
175
+ {
176
+
177
+ var f = new Form1();
178
+
179
+ f.Show();
180
+
181
+ this.Close();
182
+
183
+ }
184
+
185
+ }
186
+
187
+
188
+
189
+ ```