Darren

Darren

  • NA
  • 3
  • 0

Form.ShowDialog() closing when it SHOULDN'T be

May 8 2008 1:53 PM
I created a simple C# windows forms project in VS2005 and created a form class called, TempWindow, that simply shows itself for 2 seconds and then closes via TempWindow.ShowTemporary().  Everything works, except that when I call TempWindow.ShowTemporary() and then call RandomForm.ShowDialog(), the RandomForm gets closed too!!! This does not make any sense.

Any ideas? MUCH THANKS IN ADVANCE.


Here is the entire code below (except for the Designer.cs files)...


//*************************************************************************************************************************
//Program.cs
//*************************************************************************************************************************
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace TempWindowTester
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }
}

//*************************************************************************************************************************
//MainForm.cs (MainForm.Designer.cs was ommitted for simplicity sake)
//    This form just contains three buttons: button1, button2, button3
//*************************************************************************************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TempWindowTester
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            TempWindow tempForm = new TempWindow();
            tempForm.ShowTemporary();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            RandomForm randForm = new RandomForm();
            randForm.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //BOTH forms are closed this is incorrect!!!

            TempWindow tempForm = new TempWindow();
            tempForm.ShowTemporary();

            //This form should not be closed
            RandomForm randForm = new RandomForm();
            randForm.ShowDialog();

        }
    }
}

//*************************************************************************************************************************
//RandomForm.cs (RandomForm.Designer.cs was ommitted for simplicity sake)
//    This can be any arbitrary form
//*************************************************************************************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TempWindowTester
{
    public partial class RandomForm : Form
    {
        public RandomForm()
        {
            InitializeComponent();
        }
    }
}

//*************************************************************************************************************************
//TempWindow.cs (TempWindow.Designer.cs was omitted for simplicity sake)
//*************************************************************************************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TempWindowTester
{
    public partial class TempWindow : Form
    {
        private Timer timer = new Timer();

        public TempWindow()
        {
            InitializeComponent();
        }

        private void InitializeTimer()
        {
            timer.Interval = 2000; //Display duration of form in milliseconds
            timer.Enabled = true;

            this.timer.Tick += new EventHandler(timer_Tick);
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            //This command causes the RandomForm to close itself as well which does not make sense.
            //  this should only affect this form instance
            this.Close();
            timer.Enabled = false;
        }

        //Shows The Form Temporarily
        public void ShowTemporary()
        {
            this.Show();
            InitializeTimer();
        }      
    }
}

Answers (2)