C# 关闭并释放表单后执行的代码
我有一个Windows窗体应用程序,其运行方式我不理解。为了清晰起见,我创建了一个简单的应用程序,将功能复制到最低逻辑。它有两种形式 表格1: 有一个按钮1,单击该按钮可将行添加到数据表中。 有一个按钮2用于显示表单并将数据表发送到表单 表格2: Form2从datatable创建dataview,并将dataview绑定到listbox。Form2还有一个按钮1,用于在窗体上执行this.Close()操作。我了解到,使用formName.Show()打开的任何内容都将在.Close期间被处置 这就是事情变得奇怪的地方。每次清除数据表并再次添加行时,可以反复单击Form1按钮1。单击from1按钮2并显示表单2然后关闭后,返回表单1并单击按钮1将引发错误。错误来自form2(已关闭)列表框1\u SelectedIndexChanged事件 问题是,为什么一个控件的事件在一个窗体上触发? 我已经找到了几种方法来避免这种情况,但我想了解为什么会发生这种情况,例如设置listbox datasource=null,但我想知道发生了什么。。花了半天的时间想弄明白。所以社区,请在这里教育我 表格1代码C# 关闭并释放表单后执行的代码,c#,C#,我有一个Windows窗体应用程序,其运行方式我不理解。为了清晰起见,我创建了一个简单的应用程序,将功能复制到最低逻辑。它有两种形式 表格1: 有一个按钮1,单击该按钮可将行添加到数据表中。 有一个按钮2用于显示表单并将数据表发送到表单 表格2: Form2从datatable创建dataview,并将dataview绑定到listbox。Form2还有一个按钮1,用于在窗体上执行this.Close()操作。我了解到,使用formName.Show()打开的任何内容都将在.Close期间被处置
public partial class Form1 : Form
{
Boolean bInitialLoad = true;
DataTable dtHardware = new DataTable("Hardware");
Form2 multiServerView = new Form2();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
dtHardware.Clear();
if (bInitialLoad == true)
{
dtHardware.Columns.Add("ServerName", typeof(String));
dtHardware.Columns.Add("Environment", typeof(String));
}
DataRow drNewRow = dtHardware.NewRow();
drNewRow["ServerName"] = "SomeName";
drNewRow["Environment"] = "SomeEnvironment";
dtHardware.Rows.Add(drNewRow);
drNewRow = dtHardware.NewRow();
drNewRow["ServerName"] = "SomeName2";
drNewRow["Environment"] = "SomeEnvironment2";
dtHardware.Rows.Add(drNewRow);
bInitialLoad = false;
}
private void button2_Click(object sender, EventArgs e)
{
OpenMultiServerView();
}
private void OpenMultiServerView()
{
multiServerView = new Form2(dtHardware);
this.AddOwnedForm(multiServerView);
multiServerView.Show();
}
}
表格1设计师
namespace WindowsFormsApp4
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(308, 390);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(107, 34);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(437, 296);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(102, 33);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(679, 482);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
}
表格2设计师
namespace WindowsFormsApp4
{
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.listBox1 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(23, 113);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(749, 287);
this.dataGridView1.TabIndex = 0;
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.Location = new System.Drawing.Point(291, 31);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(171, 69);
this.listBox1.TabIndex = 1;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// button1
//
this.button1.Location = new System.Drawing.Point(653, 415);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(118, 25);
this.button1.TabIndex = 2;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.dataGridView1);
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;
}
}
namespace WindowsFormsApp4
{
部分类别表格2
{
///
///必需的设计器变量。
///
private System.ComponentModel.IContainer components=null;
///
///清理所有正在使用的资源。
///
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
///
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
///
私有void InitializeComponent()
{
this.dataGridView1=new System.Windows.Forms.DataGridView();
this.listBox1=new System.Windows.Forms.ListBox();
this.button1=new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
这个.SuspendLayout();
//
//dataGridView1
//
this.dataGridView1.columnHeadershightSizeMode=System.Windows.Forms.datagridviewColumnHeadershightSizeMode.AutoSize;
this.dataGridView1.Location=新系统.Drawing.Point(23113);
this.dataGridView1.Name=“dataGridView1”;
this.dataGridView1.Size=新系统.Drawing.Size(749287);
this.dataGridView1.TabIndex=0;
//
//列表框1
//
this.listBox1.FormattingEnabled=true;
this.listBox1.Location=新系统图点(291,31);
this.listBox1.Name=“listBox1”;
this.listBox1.Size=新系统图尺寸(171,69);
this.listBox1.TabIndex=1;
this.listBox1.SelectedIndexChanged+=新的System.EventHandler(this.listBox1\u SelectedIndexChanged);
//
//按钮1
//
this.button1.Location=新系统图纸点(653415);
this.button1.Name=“button1”;
this.button1.Size=新系统图纸尺寸(118,25);
this.button1.TabIndex=2;
this.button1.Text=“button1”;
this.button1.UseVisualStyleBackColor=true;
this.button1.Click+=新系统.EventHandler(this.button1\u Click);
//
//表格2
//
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统图尺寸(800450);
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.dataGridView1);
this.Name=“Form2”;
this.Text=“Form2”;
this.Load+=new System.EventHandler(this.Form2\u Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
此选项为.resume布局(false);
}
#端区
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button按钮1;
}
}
这是因为Form2
的实例实际上并没有消失。由于调用了Show
,但垃圾收集器尚未收集它,因此假设它已被释放是正确的。事实上,无法收集它,因为在提供的代码示例中,Form1
有两个对它创建的Form2
实例的引用
第一个是Form1.OwnedForms
。第二个是字段Form1.multiServerView
您说过有几种方法可以修复它,比如在Form2
关闭时破坏绑定,但我想我还是放弃这个建议。如果不需要每次显示时都创建一个新的Form2
实例,您可以在Form1
的构造函数中创建一个实例,处理Form2.Closing
并在用户关闭Form2
时隐藏它
所以,有点像
公共部分类表单1:表单
{
//删除bInitialLoad后,我们将在构造函数中设置数据表。
DataTable dtHardware=新的DataTable(“硬件”);
Form2 multiServerView;//没有lon
namespace WindowsFormsApp4
{
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.listBox1 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(23, 113);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(749, 287);
this.dataGridView1.TabIndex = 0;
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.Location = new System.Drawing.Point(291, 31);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(171, 69);
this.listBox1.TabIndex = 1;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// button1
//
this.button1.Location = new System.Drawing.Point(653, 415);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(118, 25);
this.button1.TabIndex = 2;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.dataGridView1);
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;
}
}