Member Avatar for micmo

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

Might I suggest you use a MDI form? It would make getting that list very easy.

As for finding the last active form, try the Activated event instead of LostFocus and GotFocus.

From MSDN:

The Enter and Leave events are suppressed by the Form class. The equivalent events in the Form class are the Activated and Deactivate events. The Enter and Leave events are hierarchical and will cascade up and down the parent chain until the appropriate control is reached. For example, assume you have a Form with two GroupBox controls, and each GroupBox control has one TextBox control. When the caret is moved from one TextBox to the other, the Leave event is raised for the TextBox and GroupBox, and the Enter event is raised for the other GroupBox and TextBox.

For your reading pleasure:
Activated
Deactiveate

Member Avatar for micmo

Thanks Zach, the Activated/Deactivated events work perfectly! I'll also have a look into the use of MDI forms.
Problem Solved! Thanks for your help! :)

Michael

Any time :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.