locked
报个vs2005的bug RRS feed

  • 问题

  •  

    我有个mdiparent窗口和一个注册到其上的一个form。

    bug:

    这个form调用hide以后再调close就不会触发这个form的formclosing事件

     

    具体程序如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    namespace WindowsApplication1
    {
        public partial class MDIParent1 : Form
        {
            private int childFormNumber = 0;

            public MDIParent1()
            {
                InitializeComponent();
                Form1 form1 = new Form1();
                form1.Show();
                form1.setForm2(this);
                form1.MdiParent = this;
            }

            private void ShowNewForm(object sender, EventArgs e)
            {
                // Create a new instance of the child form.
                Form childForm = new Form();
                // Make it a child of this MDI form before showing it.
                childForm.MdiParent = this;
                childForm.Text = "Window " + childFormNumber++;
                childForm.Show();
            }

            private void OpenFile(object sender, EventArgs e)
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
                if (openFileDialog.ShowDialog(this) == DialogResult.OK)
                {
                    string FileName = openFileDialog.FileName;
                    // TODO: Add code here to open the file.
                }
            }

            private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
                if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
                {
                    string FileName = saveFileDialog.FileName;
                    // TODO: Add code here to save the current contents of the form to a file.
                }
            }

            private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }

            private void CutToolStripMenuItem_Click(object sender, EventArgs e)
            {
                // TODO: Use System.Windows.Forms.Clipboard to insert the selected text or images into the clipboard
            }

            private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
            {
                // TODO: Use System.Windows.Forms.Clipboard to insert the selected text or images into the clipboard
            }

            private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
            {
                // TODO: Use System.Windows.Forms.Clipboard.GetText() or System.Windows.Forms.GetData to retrieve information from the clipboard.
            }

            private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e)
            {
                toolStrip.Visible = toolBarToolStripMenuItem.Checked;
            }

            private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
            {
                statusStrip.Visible = statusBarToolStripMenuItem.Checked;
            }

            private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
            {
                LayoutMdi(MdiLayout.Cascade);
            }

            private void TileVerticleToolStripMenuItem_Click(object sender, EventArgs e)
            {
                LayoutMdi(MdiLayout.TileVertical);
            }

            private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
            {
                LayoutMdi(MdiLayout.TileHorizontal);
            }

            private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
            {
                LayoutMdi(MdiLayout.ArrangeIcons);
            }

            private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
            {
                foreach (Form childForm in MdiChildren)
                {
                    childForm.Close();
                }
            }
        }
    }

     

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;

    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            private MDIParent1 form2;

            public void setForm2(MDIParent1 form2)
            {
                this.form2 = form2;
            }
            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                MessageBox.Show(this,"Form1_FormClosing");
            }

            private void button1_Click(object sender, EventArgs e)
            {
                this.Hide();
                this.Close();
                //this.Dispose();
            }
            private void InitializeComponent()
            {
                this.button1 = new System.Windows.Forms.Button();
                this.SuspendLayout();
                //
                // button1
                //
                this.button1.Location = new System.Drawing.Point(133, 91);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(75, 23);
                this.button1.TabIndex = 0;
                this.button1.Text = "button1";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                //
                // Form1
                //
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(292, 266);
                this.Controls.Add(this.button1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
                this.Load += new System.EventHandler(this.Form1_Load);
                this.ResumeLayout(false);

            }

            #endregion

            private System.Windows.Forms.Button button1;

        }
    }

     

     

    2008年7月9日 5:15

答案

  • hide后在关闭可以使用closed事件来代替closing事件.

    2008年7月16日 3:23
    版主

全部回复