c# - 代码在其所在表单被关闭并处理后执行

标签 c#

我有一个 Windows 窗体应用程序,它的运行方式我不理解。为了清晰起见,我创建了一个简单的应用程序,以最少的逻辑重现功能。它有两种形式。

表格1: 有一个按钮 1,单击该按钮可将行添加到数据表中。 有一个按钮2,用于显示表单并将数据表发送到表单。

表格2: Form2 从数据表创建数据 View 并将数据 View 绑定(bind)到列表框。 Form2 还有一个在表单上执行 this.Close() 的button1。我读到用 formName.Show() 打开的任何内容都将在 .Close 期间被处理。

这就是事情变得奇怪的地方。每次清除数据表并再次添加行时,都可以反复单击 Form1 按钮 1。一旦单击 from1 按钮 2,显示表单 2,然后关闭,返回到表单 1 并单击按钮 1 会引发错误。该错误来自 form2(已关闭)listBox1_SelectedIndexChanged 事件。

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

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 设计器

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

最佳答案

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

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

您说过您有几种方法可以修复它,例如在 Form2 关闭时打破绑定(bind),但我想我应该放弃这个建议。如果您实际上不需要每次显示时都创建 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();
        }
    }
}

这样,Form1 就只有一个 Form2 实例。您不必担心 Form2 的实例已被释放但尚未收集,但当 Form1 上的某些内容更改数据源时会触发事件。

关于c# - 代码在其所在表单被关闭并处理后执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56563594/

相关文章:

c# - Windows 10 上的 MSHTML DLL

c# - 编写内联事件处理程序是不好的做法吗

c# - 如何从 WCF 中的客户端消息检查器获取 header 值

c# - 如何访问USB设备中的文件?

c# - 在 IIS 中设置应用程序池的优先级

c# - ASP.Net MVC 纯 View 模型

c# - 自定义验证器是否在 FormView 中工作?

c# - Entity Framework 代码优先和链接的实体

c# - 具有依赖注入(inject)的基本 Controller 的设计模式 - MVC 3 + Ninject

c# - 捕获从 .NET 应用程序执行的 powershell 脚本的标准输出