Обнаружил с трудом, (искомое находится в последнем кармане :))
Есть MDIParent2 : Form (MDIContainer=true) и Form2:Form открывающаяся как MDIChild
для MDIParent2 обрабатываем событие MDIChildActivate:
1.
2.
3.
4.
5.
6.
7.
8.
9.
private void MDIParent2_MdiChildActivate(object sender, EventArgs e)
{
//if (this.ActiveMdiChild == null)
//return;
if (this.ActiveMdiChild.GetType() == typeof(Form2))
{
//
}
}
При закрытии открытой в единственном экземпляре Form2 тоже возникает событие MDIActivateChild. При этом ActiveMDIChild имеет значение null. Однако, вызов this.ActiveMdiChild.GetType() реанимирует ссылку на уходящее в мусор MDIChild окно. Это видно, если вставить MessageBox в Closing этого окна. Если снять комментарий с проверки на null (то есть не вызывать this.ActiveMdiChild.GetType()) зомби-окошко не образуется.
Вопрос: это нормально?
Пример для воспроизведения:
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.
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
static class Program
{
/// <summary>
/// Главная точка входа для приложения.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MDIParent2());
}
}
public partial class MDIParent2 : Form
{
private int childFormNumber = 0;
public MDIParent2()
{
InitializeComponent();
}
private void ShowNewForm(object sender, EventArgs e)
{
Form childForm = new Form2();
childForm.MdiParent = this;
childForm.Text = "Окно " + childFormNumber++;
childForm.Show();
}
private void MDIParent2_MdiChildActivate(object sender, EventArgs e)
{
//if (this.ActiveMdiChild == null)
//return;
if (this.ActiveMdiChild.GetType() == typeof(Form2))
{
//ToolStripManager.Merge(((TSqlFrm)this.ActiveMdiChild).tsTSQL, "toolStrip");
}
}
private void InitializeComponent()
{
this.menuStrip = new System.Windows.Forms.MenuStrip();
this.fileMenu = new System.Windows.Forms.ToolStripMenuItem();
this.menuStrip.SuspendLayout();
this.SuspendLayout();
//
// menuStrip
//
this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.fileMenu});
this.menuStrip.Location = new System.Drawing.Point(0, 0);
this.menuStrip.Name = "menuStrip";
this.menuStrip.Padding = new System.Windows.Forms.Padding(8, 2, 0, 2);
this.menuStrip.Size = new System.Drawing.Size(843, 28);
this.menuStrip.TabIndex = 0;
this.menuStrip.Text = "MenuStrip";
//
// fileMenu
//
this.fileMenu.ImageTransparentColor = System.Drawing.SystemColors.ActiveBorder;
this.fileMenu.Name = "fileMenu";
this.fileMenu.Size = new System.Drawing.Size(57, 24);
this.fileMenu.Text = "&Файл";
this.fileMenu.Click += new System.EventHandler(this.ShowNewForm);
//
// MDIParent1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(843, 558);
this.Controls.Add(this.menuStrip);
this.IsMdiContainer = true;
this.MainMenuStrip = this.menuStrip;
this.Margin = new System.Windows.Forms.Padding(4);
this.Name = "MDIParent2";
this.Text = "MDIParent2";
this.MdiChildActivate += new System.EventHandler(this.MDIParent2_MdiChildActivate);
this.menuStrip.ResumeLayout(false);
this.menuStrip.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.MenuStrip menuStrip;
private System.Windows.Forms.ToolStripMenuItem fileMenu;
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
messageBoxCS.AppendFormat("{0} = {1}\n{2}={3}", "CloseReason", e.CloseReason, "IsDisposed", this.IsDisposed);
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "Cancel", e.Cancel);
messageBoxCS.AppendLine();
MessageBox.Show(messageBoxCS.ToString(), "FormClosing Event");
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(446, 365);
this.Name = "Form2";
this.Text = "Form2";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form2_FormClosing);
this.ResumeLayout(false);
}
}
}