本文介绍了表单激活和停用事件中存在问题(Windows应用程序,.Net 4.0).的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景
这是非MDI Windows应用程序.
我有一个家庭表单,其中包含一个名为Panel1的面板以及两个按钮btnForm1和btnForm2.单击btnForm1和btnForm2分别在Panel1中打开Form1和Form2.在Panel1中打开表单之前,先清除Panel1中所有打开的表单.代码如下:

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:

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



在Form1中,有一个按钮btnForm3,它在Form2上方的frmHome.Panel1中打开Form3,而不会清除Panel1中的任何现有表单.这不是由
完成的直接使用btnForm3_Click事件,而在btnForm3_Click中引发以这种形式声明的朋友事件,该事件在frmHome中处理. frmHome中的代码
处理如下:



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:

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



所有这四个表单在它们的所有工具快捷键中都具有添加",编辑"和删除"按钮.他们还具有文件菜单
它们共同的菜单项.

目标
将frmHome.Panel1中当前活动窗体的菜单和工具栏合并到frmHome的菜单和工具栏.当面板的活动形式被停用时,要从frmHome的菜单和工具条中取消frmHome.Panel1中当前活动形式的菜单和工具条.可以通过在frmHome.Panel1:
中打开的表格中包括以下各行来解决此问题. 合并



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

ToolStripManager.Merge(Me.MenuStrip1, frmHome.MenuStrip1)
ToolStripManager.Merge(Me.ToolStrip1, frmHome.ToolStrip1)



要取消合并



To unmerge

ToolStripManager.RevertMerge(frmHome.MenuStrip1)
ToolStripManager.RevertMerge(frmHome.ToolStrip1)



问题
以上几行应写在哪些事件中?
在MDI应用程序中,可以通过将它们分别包含在Form_Activated和Form_Deactivate事件中来实现.但是这里既没有激活也没有
当您在frmHome.Panel1中打开表单时,将停用事件.在激活和停用后,您可以使用Form_Load和Form_FormClosed事件,但是只有在关闭现有表单之前在面板中打开新表单之前,它们才会合并/取消合并菜单.但是由于有时我需要打开和关闭表单,并保持现有表单处于打开状态,因此使用这些事件将无法完成任务.甚至GotFocus和LostFocus事件也不会起作用.因此,我希望激活事件和取消激活事件被触发,或者当窗体分别获得/失去焦点时,可以通过某些其他方式来合并/取消菜单和工具栏.这真让我抓狂.我根本找不到出路.请帮忙.问候.



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. Instaed 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.

推荐答案

frmSubForm1.Close()
frmSubForm1=Nothing


但是UserControls没有任何Close()方法.

如有需要,请随意继续此线程,并提出您的建议.


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

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



这篇关于表单激活和停用事件中存在问题(Windows应用程序,.Net 4.0).的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 15:14