Question Problem in Form Activated and Deactivate events (Windows app, .Net 4.0).

priyamtheone

Well-known member
Joined
Sep 20, 2007
Messages
96
Programming Experience
Beginner
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:
VB.NET:
Private objForm1 As Form1
Private Sub btnForm1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForm1.Click
	objForm1 = New Form1
	objForm1.FormBorderStyle = Windows.Forms.FormBorderStyle.None
       objForm1.TopLevel = False
       objForm1.Dock = DockStyle.Fill
	Panel1.Controls.Clear
	Panel1.Controls.Add(objForm1)
	objForm1.Show()
End Sub

Private Sub btnForm2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForm2.Click
	Dim objForm2 As New Form1
	objForm2.FormBorderStyle = Windows.Forms.FormBorderStyle.None
       objForm2.TopLevel = False
       objForm2.Dock = DockStyle.Fill
	Panel1.Controls.Clear
	Panel1.Controls.Add(objForm2)
	objForm2.Show()
End Sub

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 to handle it follows:
VB.NET:
Private Sub objForm1_LoadForm3InHome() Handles objForm1.LoadSubForm3InHome
	Dim objForm3 As New Form3
	objForm3.FormBorderStyle = Windows.Forms.FormBorderStyle.None
       objForm3.TopLevel = False
       objForm3.Dock = DockStyle.Fill
       Panel1.Controls.Add(objForm3)
       objForm3.Show()
       objForm3.BringToFront()
End Sub

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
VB.NET:
ToolStripManager.Merge(Me.MenuStrip1, frmHome.MenuStrip1)
ToolStripManager.Merge(Me.ToolStrip1, frmHome.ToolStrip1)

To unmerge
VB.NET:
ToolStripManager.RevertMerge(frmHome.MenuStrip1)
ToolStripManager.RevertMerge(frmHome.ToolStrip1)

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.
 
I have never tried it, but would this be of any use :-

VB.NET:
    Private Sub FormAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles Panel1.ControlAdded
    End Sub

    Private Sub FormRemoved(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles Panel1.ControlRemoved
    End Sub
 
Back
Top