表单输入后的代码已关闭并处置

时间:2019-06-12 13:37:29

标签: c#

我有一个Windows Forms App,它以我不了解的方式运行。我创建了一个简单的应用程序,以简化功能,以最小的逻辑再现功能。它有两种形式。

Form1: 有一个button1,单击该按钮时会将行添加到数据表中。 有一个button2,它显示一个表单并将数据表发送到该表单。

Form2: Form2从数据表创建一个数据视图,并将该数据视图绑定到列表框。 Form2也有一个button1,它在表单上执行this.Close()。我读到任何用formName.Show()打开的东西都将在.Close期间处理。

在这里,事情变得很奇怪。每次都可以单击Form1 button1以清除数据表并再次添加行。单击from1 button2之后,将显示form2,然后将其关闭,然后返回到form1,然后单击button1会引发错误。错误是来自form2(已关闭)listBox1_SelectedIndexChanged事件。

问题是,为什么要为已消失的窗体上已消失的控件触发事件? 我已经找到了几种避免这种情况的方法,但是我想了解为什么会发生这种情况,例如将listbox设置为datasource = null,但想知道发生了什么。。花了半天时间试图弄清楚这一点。这样的社区,请在这里教育我。

Form1代码

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();
    }
}

Form1设计器

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;
    }
}

Form2代码

public partial class Form2 : Form
{

    DataView dvServers = null;

    public Form2()
    {
        InitializeComponent();
    }

    public Form2(DataTable dt1)
    {
        InitializeComponent();

        dvServers = new DataView(dt1);
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        listBox1.DisplayMember = "ServerName";
        listBox1.DataSource = dvServers;
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        System.Windows.Forms.FormCollection fc = Application.OpenForms;
        foreach (System.Windows.Forms.Form frm in fc)
        {
            if (frm.Name == "Form2")
            {
                return;
            }
        }
        MessageBox.Show("I am the listbox event from Form2");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}

Form2 Designer

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;
    }
}

1 个答案:

答案 0 :(得分:2)

这是因为Form2的实例实际上并未消失。自从您调用Show以来,您就假设它已被处置是正确的,但尚未被垃圾收集器收集。实际上,无法收集它,因为在提供的代码示例中,Form1对创建的Form2实例有两个引用。

第一个在Form1.OwnedForms中。第二个是字段Form1.multiServerView

您说过,您有几种方法可以解决此问题,例如在Form2关闭时取消绑定,但是我认为我只是放弃了这一建议。如果实际上不需要每次显示时都创建一个Form2的新实例,则可以在Form1的构造函数中创建它的一个实例,处理Form2.Closing并仅当用户关闭Form2时将其隐藏。

所以,类似...

public partial class Form1 : Form
{
    //Removed bInitialLoad, we'll set up the data table in the constructor.
    DataTable dtHardware = new DataTable("Hardware");
    Form2 multiServerView; //No longer initalizing here, we'll do that in the constructor.

    public Form1()
    {
        InitializeComponent();

        dtHardware.Columns.Add("ServerName", typeof(String));
        dtHardware.Columns.Add("Environment", typeof(String));
        multiServerView = new Form2(dtHardware);
        AddOwnedForm(multiServerView);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        dtHardware.Clear();

        //Removed the check of bInitialLoad.

        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);

        //Removed setting bInitialLoad.

    }

    private void button2_Click(object sender, EventArgs e)
    {
        OpenMultiServerView();
    }

    private void OpenMultiServerView()
    {
        multiServerView.Show(); //Just show it.
    }
}

public partial class Form2 : Form
{

    DataView dvServers = null;

    //Removed the empty constructor since Form1 no longer needs it.

    public Form2(DataTable dt1)
    {
        InitializeComponent();

        dvServers = new DataView(dt1);
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        listBox1.DisplayMember = "ServerName";
        listBox1.DataSource = dvServers;
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        MessageBox.Show("I am the listbox event from Form2");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void Form2_Closing(object sender, FormClosingEventArgs e)
    {
        //Don't forget to wire it up in the designer!
        //If the user clicks "button1" or the "X" then just hide the form.
        if (e.CloseReason == CloseReason.UserClosing)
        {
            e.Cancel = true;
            Hide();
        }
    }
}

这样,Form2的{​​{1}}实例永远只有一个。您不必担心Form1上的某些实例更改了数据源时已处置但尚未收集的Form2实例却触发了事件。