Scenario
This is a non-MDI windows application.
I have a home form containing a panel named Panel1 and two buttons btnForm1 and btnForm2. Clicking btnForm1 and btnForm2 opens up Form1 and
Form2 respectively in Panel1. Before a form is opened in Panel1, all opened forms in Panel1 are cleared. The code follows:

In Form1 there's a button btnForm3 that opens up Form3 in frmHome.Panel1 just above Form2 without clearing any existing forms in Panel1. This is not done by btnForm3_Click event directly, rather a friend event declared in this form is raised in btnForm3_Click which is handled in frmHome. The code in frmHome tohandle it follows:

All the four forms have Add, Edit and Delete buttons among their toolstripbuttons that are common to all of them. They also have File menu among their menuitems that are common to them.

Objective
To merge the menu and toolstrip of the currently active form in frmHome.Panel1 to the menu and toolstrip of frmHome. To unmerge the menu and toolstrip of the currently active form in frmHome.Panel1 from the menu and toolstrip of frmHome when the active form of the panel is deactivated.
This can be handled by including the undermentioned lines in the form that will open in frmHome.Panel1:To merge

Problem
In which events should the above lines be written?
In MDI apps this could have been accomplished by including them in Form_Activated and Form_Deactivate events respectively. But here neither Activated nor Deactivate events fire when you open forms in frmHome.Panel1. Instead of Activated and Deactivate you can use Form_Load and Form_FormClosed events but they will only merge/unmerge menus if existing forms are closed before opening a new form in the panel. But as I sometimes need forms to be opened and closed keeping existing forms opened, using these events won't fulfill the task. Even the GotFocus and LostFocus events won't work. So I want Activated and Deactivate events to be fired or some other means by which the menus and toolstrips can be merged/unmerged when the form gains/looses focus respectively. This is driving me crazy. I can't at all find a way out. Please help. Regards.

The forms GotFocus and LostFocus events don't work, because the user tends to click on one of the forms controls, not the form itself.

The only thing I can think of is to setup several functions. One function in each form that handles the GotFocus event for that form and every one of its controls.

The GotFocus function would call A friend Function and pass it the form name. The friend function Needs a static variable that holds the name of the last form that had the focus. Then it would check to see if the form name in LastForm matches the one sent to the function. If it does then exit the function, otherwise execute the proper unmerge for the LastForm and then the merge for the NewForm, and set LastForm = NewForm.

If you need to do the merge and unmerge when a form is loaded or unloaded then you could use each forms Load and Unload events to call the Firend Function and pass the Form name.

When unloading add something simple in front of the form name, like an asterisk, so the friend function knows the form is unloading and will simply do the unmerge and set LastForm to blank. This way the next time the Friend Function is called it will know that if the LastForm is blank then the unmerge won't need to be done.

04-05-2011, 11:21 AM

priyamtheone

Quote:

Originally Posted by Ron Weller

FYI: shouldn't the form object be set to form2, not form1??

Oh! That's a typing mistake. U r right. :o

04-05-2011, 11:27 AM

priyamtheone

Quote:

Originally Posted by Ron Weller

If you need to do the merge and unmerge when a form is loaded or unloaded then you could use each forms Load and Unload events to call the Firend Function and pass the Form name.

If I'm not wrong, the Load and Unload events don't work for the forms that are opening in the Panel. The reason I came to know is those forms behave as normal UserControls instead of forms when they are opened in the Panel.

I'll work with your idea and get back soon.

04-05-2011, 11:56 AM

Ron Weller

Quote:

Originally Posted by priyamtheone

If I'm not wrong, the Load and Unload events don't work for the forms that are opening in the Panel.

Then couldn't you just call the friend function from the button click event that triggers the addition and also removal of the form?

04-07-2011, 12:15 PM

priyamtheone

Ok, I worked on it and my conclusion is, the best way to handle this is to use the ControlAdded and ControlRemoved events of the splitcontainer to check which control is currently added/removed and merge/unmerge the toolstrip of that control w.r.t. the Home form toolstrip. Just write a global method to merge/unmerge the toolstrips with a parameter for the name of the control (the control that'll be added/removed in the splitcontainer). In the ControlAdded and ControlRemoved events, call the method with the child control as argument. Refactoring the subforms that I mentioned in the OP to UserControls improved performance but in certain cases they don't fulfill my requirement e.g. when Subform1 is opened in the splitcontainer and Subform3 is opened immediately from it without focusing on any other control of SubForm1 (suppose by clicking a toolstripbutton on SubForm1), unmerging doesn't occur as events like LostFocus, Leave etc are not fired. This creates ambiguity in the menus. So I'll go for the Splitcontainer ControlAdded/ControlRemoved events. But instead of opening forms as child controls I'll henceforth use UserControls.
I'll drop a small question at the end. How'll I close the UserControl from the SplitContainer panel? Till now I was using:

Code:

frmSubForm1.Close()
frmSubForm1=Nothing

But UserControls don't have any Close() method.

Fell free to continue this thread further with your suggestions if needed.

04-08-2011, 10:20 PM

JBourgeois

Sorry, I am in a rush and do not have time to read all the posts in details, but I immediately see a lot of errors that I encounter regularly in my training sessions.

I might be out of topic, but if it helps.

In order for a form to receive the GotFocus and/or LostFocus events, the form should not contain any control that can receive the focus. This would happen for instance for a form that has only labels. As soon as a form has 1 control that can receive focus (TextBox, Button), the form will never receive the focus events. The control will.

Someone somewhere talks about an Unload event. Sorry, we are not in VB6 anymore. There is no Unload event in .NET. It equivalent it somewhere between FormClosing and FormClosed.