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
{
///
/// The main entry point for the application.
///
[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();
}
}
}
DarrenPosted May 8, 2008, 11:33 PM
PERFECT. You were right on. Thanks for taking the time to solve my problem.
Darren
AlanPosted May 8, 2008, 3:42 PM
What's happening here is that when you call randForm.ShowDialog() this displays the random form modally but with the currently active form i.e. tempForm as its owner.
When tempForm is closed after two seconds this also closes the owned form, randForm.
To avoid this happening either use randForm.Show() or randForm.ShowDialog(this) to make the main form the owner.