Hi Guys,
I have two forms (Main and Sub). There will only ever be one instance of Main, but can be many open windows of the Sub form.
What I want to do is know which Sub form was the last form to have focus when switching to the Main window.
Scenario: A user starts of in the form Main, then opens three Sub forms using the same button (forms have Titles (form.Text) Sub1, Sub2, Sub3). After opening Sub3, they click back on Sub2 (so the last Sub form is Sub2). They then click back onto Main, and so the last Sub form is still Sub2.
I have tried adding a "LostFocus" event handler to the Sub form so that when the form looses focus it shows a messagebox (just to see if it works) - but NOTHING happens. It's the same with GotFocus:
public partial class Sub : Form
{
public Sub()
{
InitializeComponent();
this.GotFocus +=new EventHandler(Sub_GotFocus);
this.LostFocus +=new EventHandler(Sub_LostFocus);
}
private void Sub_GotFocus(object sender, EventArgs e)
{
MessageBox.Show("'" + this.Text + "' got focus");
}
private void Sub_LostFocus(object sender, EventArgs e)
{
MessageBox.Show("'" + this.Text + "' lost focus");
}
}
Is what I want to do even possible?
My end-game is to have a drop-down list on the Main form that lists all of the open Sub windows, and when you click on an item it brings that form to the front by doing the following:
//Add the forms to the dropdown list
foreach (Form form in Application.OpenForms)
{
if (form.Name == "Sub")
{
//Add the form name to the dropdown list
//button.Text = form.Text; //The form title
//button.Click += new EventHandler(switchWindow);
}
}
//When the user clicks on a dropdown list item:
private void switchCaseWindow(object sender, EventArgs e)
{
ToolStripMenuItem item = (ToolStripMenuItem)sender;
int index = getOpenCaseForm(item.Text); //returns the form index from Application.OpenForms or -1 if the form is not open
if (index != -1)
Application.OpenForms[index].BringToFront();
}
Any help will be greatly appreciated!
Michael