动态创建的菜单和mdi孩子 [英] Dynamically-created menus and mdi children

查看:51
本文介绍了动态创建的菜单和mdi孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前发布了类似的消息,但我们已经稍微偏离了

原创主题,所以我想我会再次发布,希望能获得

来自他人的输入。


我正在动态创建一个MenuStrip。对于每个ToolStripMenuItem,我是
添加一个事件处理程序,以便无论

单击哪个菜单项,都会调用相同的例程。所有这一切都很棒 - 我的菜单正在正确加载
并正常调用我的例程。


在每个ToolStripMenuItem的Tag属性中我存储名称一个

表单,我想打开作为我的主要表单的mdi孩子。我无法让b $ b让这个工作正常,所以出于测试目的,我已经将我的

问题减少到下面的场景(这与我们无关

Tag属性或我的表单名称存储在

变量中的事实:


在我创建的主表单后面以下例程:


Private Sub LoadForm()

Dim frm As New frmTest

with frm

.MdiParent =我

.Left = 0

。顶部= 0

。显示()

结束与

结束子


我还在我的表单和按钮的点击事件中添加了一个按钮,

我调用上面的例程。


在单击菜单项时调用的例程中,我调用了

相同的LoadForm例程。


如果我点击我的按钮,frmTest会加载为mdi孩子。

如果我点击一个菜单项,我的表单就不会被加载(是的,我' '$

conf这个例行程序被调用了。

顺便说一句,如果我注释掉.MdiParent = Me的行,我的

表格将作为正常的Windows窗体。但是,只要我b / b
取消注释该行,我的表格就不会加载。


我很困惑为什么这么简单的事情会单击

按钮时工作,但是当我点击菜单项时没有工作。


顺便说一下,我尝试加载带有静态菜单项的菜单(而不是

动态加载它,一切正常 - 我的mdi子表格

正确加载。


任何帮助都会是非常感谢。


谢谢,


史蒂夫

I posted a similar message earlier but we''ve geared away from the
original topic somewhat so I thought I''d post again in hopes of getting
input from others.

I am creating a MenuStrip dynamically. For each ToolStripMenuItem, I''m
adding an event handler so that the same routine gets called regardless
of which menu item was clicked. All of this works great - my menu is
being loaded properly and my routine gets called properly.

Within the Tag property of each ToolStripMenuItem I store the name of a
form that I want opened as an mdi child of my main form. I couldn''t
get this to work properly, so for testing purposes I''ve reduced my
problem to the following scenario (which really has nothing to do with
the Tag property or the fact that my form names are stored in
variables):

Behind my main form I created the following routine:

Private Sub LoadForm()
Dim frm As New frmTest
With frm
.MdiParent = Me
.Left = 0
.Top = 0
.Show()
End With
End Sub

I also added a button to my form and in the click event of the button,
I call the above routine.

In my routine that gets called when a menu item is clicked, I call the
same LoadForm routine.

If I click my button, frmTest loads as an mdi child as it should.
If I click a menu item, my form does not get loaded (and yes, I''ve
confirmed that the routine is getting called).
Incidentally, if I comment out the line that says ''.MdiParent = Me'', my
form will open as a normal Windows form. However, as soon as I
uncomment out that line, my form will not load.

I''m puzzled as to why such a simple thing would work when I click a
button but not when I click a menu item.

As an aside, I tried loading a menu with static menu items (instead of
loading it dynamically), and everything worked fine - my mdi child form
loaded properly.

Any help would be greatly appreciated.

Thanks,

Steve

推荐答案

嗨Steve,


显示你将处理程序挂钩到菜单项的代码段。

是否在MDIParent Load子程序中完成?


-tom

hi Steve,

show the piece of code where you hook the handlers to the menuitems.
Is it done within the MDIParent Load sub?

-tom


嗨汤姆,


以下两个例程用于加载我的主菜单。第一个

一个用于我的主要项目,第二个被递归调用

来加载所有子项目。在加载

我的主窗体期间调用第一个例程。

Public Sub LoadMainMenu(ByVal prmMenu As MenuStrip)

Dim con As SqlConnection

Dim cmd As SqlCommand

Dim drd As SqlDataReader

Dim strSQL As String


prmMenu.Items.Clear()


con =新的SqlConnection(THAGlobal.ConnectString)

如果是IsECCI()那么

STRSQL = QUOT; SELECT MenuItemID,MenuItemName,

MenuItemFormName,MenuItemFunctionName,MenuItemECCIOnlyFlag FROM

的MenuItems WHERE(MenuItemDisplayFlag = 1)和(SubMenuOf IS NULL)令

。通过MenuItemDisplayOrder"

,否则

STRSQL = QUOT; SELECT MenuItemID,MenuItemName,

MenuItemFormName,MenuItemFunctionName,MenuItemECCIOnlyFlag FROM

MenuItems WHERE(MenuItemDisplayFlag = 1)和(SubMenuOf为Null)ORDER

BY MenuItemDisplayOrder"

结束如果

cmd =新的SqlCommand(st rSQL)

cmd.Connection = con

尝试

con.Open()

Catch ex As Exception < br $>
退出Sub

结束尝试

drd = cmd.ExecuteReader


尝试

Do drd.Read

Dim tsi As New ToolStripMenuItem


with tsi

.Name = drd(" MenuItemID")

.Text = drd(" MenuItemName")

.Tag = vbNullString

如果是drd(" MenuItemFormName")。 ToString<> vbNullString

然后

.Tag =" F_" &安培; drd(" MenuItemFormName")。ToString

ElseIf drd(" MenuItemFunctionName")。ToString<>

vbNullString然后

。 Tag =" U_" &

drd(" MenuItemFunctionName")。ToString

结束如果

如果drd(" MenuItemECCIOnlyFlag")= 1则

.ForeColor = Color.Red

结束如果

结束

如果tsi.Tag<> vbNullString然后

AddHandler tsi.Click,AddressOf

frmMain.MainMenuClicked

结束如果

调用LoadMenuItems(tsi)

prmMenu.Items.Add(tsi)

循环

drd.Close()


Catch ex As Exception

最后

con.Close()

结束尝试

End Sub


Private Sub LoadMenuItems(ByVal prmParent As ToolStripMenuItem)

Dim con As SqlConnection

Dim cmd As SqlCommand

Dim drd As SqlDataReader

Dim strSQL As String


con = New SqlConnection(THAGlobal.ConnectString)

If IsECCI()Then <登记/>
STRSQL = QUOT; SELECT MenuItemID,MenuItemName,

MenuItemFormName,MenuItemFunctionName,MenuItemECCIOnlyFlag FROM

的MenuItems WHERE(MenuItemDisplayFlag = 1)和(SubMenuOf = QUOT ;&

prmParent.Name&")ORDER BY MenuItemDisplayOrder"

Else

strSQL =" SELECT Me nuItemID,MenuItemName,

MenuItemFormName,MenuItemFunctionName,MenuItemECCIOnlyFlag FROM

MenuItems WHERE(MenuItemDisplayFlag = 1)And(SubMenuOf =" &

prmParent.Name& ")和(MenuItemECCIOnlyFlag = 0)ORDER BY

MenuItemDisplayOrder"

结束如果

cmd =新的SqlCommand(strSQL)

cmd.Connection = con

尝试

con.Open()

Catch ex As Exception

退出Sub

结束尝试


drd = cmd.ExecuteReader


尝试

do drd.Read

Dim tsi As New ToolStripMenuItem

with tsi

.Name = drd(" MenuItemID")

.Text = drd(" MenuItemName")

如果是drd(" MenuItemFormName")。ToString<> vbNullString

然后

.Tag =" F_" &安培; drd(" MenuItemFormName")。ToString

ElseIf drd(" MenuItemFunctionName")。ToString<>

vbNullString然后

。 Tag =" U_" &

drd(" MenuItemFunctionName")。ToString

结束如果

如果drd(" MenuItemECCIOnlyFlag")= 1则

.ForeColor = Color.Red

结束如果

结束

如果tsi.Tag<> vbNullString然后

AddHandler tsi.Click,AddressOf

frmMain.MainMenuClicked

结束如果

调用LoadMenuItems(tsi)

prmParent.DropDownItems.Add(tsi)

循环

drd.Close()


Catch ex As Exception

最后

con.Close()

结束尝试

End Sub


您可以看到我在菜单的Click事件中添加了一个事件处理程序

项目(仅限相关项目)。 Click事件调用以下

例程,它位于我的主窗体中。


Public Sub MainMenuClicked(ByVal sender As System.Object,ByVal e

As System.EventArgs)

调用LoadForm()

结束子


LoadForm看起来像这样:


Private Sub LoadForm()

Dim frm As new frmParticipantSearch

with frm

。 MdiParent =我

.Left = 0

。顶部= 0

。显示()

结束

结束子


我还添加了一个包含以下代码的按钮:


Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理Button1.Click

调用LoadForm()

End Sub


正如我之前所说,如果我点击我的按钮everthing工作正常 - 我的表格

作为一个mdi孩子加载。


我我确认我的MainMenuClicked事件在我获得时被调用b $ b点击任何分配了标签的菜单项,应该如此。如果

我在运行时使用断点逐步执行代码,它将调用

LoadForm并处理每一行没有错误,但我不会看到我的

表格。


我从这里删除它,但我之前添加了事件处理程序到我测试的

Load和FormClosed事件形式只是为了看表格是否已经加载并立即关闭,但是这些事件并没有发生(他们

是当我点击我的按钮时发生火灾的消息框)然后

关闭了我的测试表格。


最让我感到惊讶的是,如果我注释掉那条线路,那就是
使我的测试形成一个mdi孩子,它的工作原理。如果通过菜单调用,我就不能将我的测试

表格指定为mdi孩子。


此外,我添加了一个静态菜单,然后单击其中一个名为LoadForm的项目。一切正常。


再次,感谢您的帮助。


史蒂夫

Hi Tom,

The following two routines are used to load my main menu. The first
one is for my "main" items, and the second one gets called recursively
to load all subitems. The first routine gets called during the load of
my main form.

Public Sub LoadMainMenu(ByVal prmMenu As MenuStrip)
Dim con As SqlConnection
Dim cmd As SqlCommand
Dim drd As SqlDataReader
Dim strSQL As String

prmMenu.Items.Clear()

con = New SqlConnection(THAGlobal.ConnectString)
If IsECCI() Then
strSQL = "SELECT MenuItemID, MenuItemName,
MenuItemFormName, MenuItemFunctionName, MenuItemECCIOnlyFlag FROM
MenuItems WHERE (MenuItemDisplayFlag=1) And (SubMenuOf Is Null) ORDER
BY MenuItemDisplayOrder"
Else
strSQL = "SELECT MenuItemID, MenuItemName,
MenuItemFormName, MenuItemFunctionName, MenuItemECCIOnlyFlag FROM
MenuItems WHERE (MenuItemDisplayFlag=1) And (SubMenuOf Is Null) ORDER
BY MenuItemDisplayOrder"
End If
cmd = New SqlCommand(strSQL)
cmd.Connection = con
Try
con.Open()
Catch ex As Exception
Exit Sub
End Try
drd = cmd.ExecuteReader

Try
Do While drd.Read
Dim tsi As New ToolStripMenuItem

With tsi
.Name = drd("MenuItemID")
.Text = drd("MenuItemName")
.Tag = vbNullString
If drd("MenuItemFormName").ToString <> vbNullString
Then
.Tag = "F_" & drd("MenuItemFormName").ToString
ElseIf drd("MenuItemFunctionName").ToString <>
vbNullString Then
.Tag = "U_" &
drd("MenuItemFunctionName").ToString
End If
If drd("MenuItemECCIOnlyFlag") = 1 Then
.ForeColor = Color.Red
End If
End With
If tsi.Tag <> vbNullString Then
AddHandler tsi.Click, AddressOf
frmMain.MainMenuClicked
End If
Call LoadMenuItems(tsi)
prmMenu.Items.Add(tsi)
Loop
drd.Close()

Catch ex As Exception
Finally
con.Close()
End Try
End Sub

Private Sub LoadMenuItems(ByVal prmParent As ToolStripMenuItem)
Dim con As SqlConnection
Dim cmd As SqlCommand
Dim drd As SqlDataReader
Dim strSQL As String

con = New SqlConnection(THAGlobal.ConnectString)
If IsECCI() Then
strSQL = "SELECT MenuItemID, MenuItemName,
MenuItemFormName, MenuItemFunctionName, MenuItemECCIOnlyFlag FROM
MenuItems WHERE (MenuItemDisplayFlag=1) And (SubMenuOf = " &
prmParent.Name & ") ORDER BY MenuItemDisplayOrder"
Else
strSQL = "SELECT MenuItemID, MenuItemName,
MenuItemFormName, MenuItemFunctionName, MenuItemECCIOnlyFlag FROM
MenuItems WHERE (MenuItemDisplayFlag=1) And (SubMenuOf = " &
prmParent.Name & ") And (MenuItemECCIOnlyFlag = 0) ORDER BY
MenuItemDisplayOrder"
End If
cmd = New SqlCommand(strSQL)
cmd.Connection = con
Try
con.Open()
Catch ex As Exception
Exit Sub
End Try

drd = cmd.ExecuteReader

Try
Do While drd.Read
Dim tsi As New ToolStripMenuItem
With tsi
.Name = drd("MenuItemID")
.Text = drd("MenuItemName")
If drd("MenuItemFormName").ToString <> vbNullString
Then
.Tag = "F_" & drd("MenuItemFormName").ToString
ElseIf drd("MenuItemFunctionName").ToString <>
vbNullString Then
.Tag = "U_" &
drd("MenuItemFunctionName").ToString
End If
If drd("MenuItemECCIOnlyFlag") = 1 Then
.ForeColor = Color.Red
End If
End With
If tsi.Tag <> vbNullString Then
AddHandler tsi.Click, AddressOf
frmMain.MainMenuClicked
End If
Call LoadMenuItems(tsi)
prmParent.DropDownItems.Add(tsi)
Loop
drd.Close()

Catch ex As Exception
Finally
con.Close()
End Try
End Sub

You can see that I added an Event Handler to the Click event of my menu
items (only the relevant ones). The Click event calls the following
routine, which is located in my main form.

Public Sub MainMenuClicked(ByVal sender As System.Object, ByVal e
As System.EventArgs)
Call LoadForm()
End Sub

LoadForm looks like this:

Private Sub LoadForm()
Dim frm As New frmParticipantSearch
With frm
.MdiParent = Me
.Left = 0
.Top = 0
.Show()
End With
End Sub

I also added a button with the following code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Call LoadForm()
End Sub

As I said before, if I click my button everthing works fine - my form
loads as an mdi child.

I''ve confirmed that my MainMenuClicked event is getting called when I
click on any menu item that has a Tag assigned to it, as it should. If
I step through the code at runtime using breakpoints, it''ll call
LoadForm and process every line with no errors, but I won''t see my
form.

I removed it from here, but I previously added event handlers to the
Load and FormClosed events of my test form just to see if the form was
being loaded and immediately closed, but those events didn''t fire (they
were MsgBoxs that did fire when I clicked my button and subsequently
closed my test form).

The thing that gets me most is that if I comment out the line that
makes my test form an mdi child, it works. I just can''t assign my test
form as an mdi child if it''s being called via the menu.

Also, I added a static menu and behind the click of one of the items I
called LoadForm. Everything worked.

Again, thanks for assistance.

Steve


让我们做第一次尝试。尝试更换:


AddHandler tsi.Click,AddressOf frmMain.MainMenuClicked()


with


RemoveHandler tsi.Click,AddressOf frmMain.MainMenuClicked()

AddHandler tsi.Click,AddressOf frmMain.MainMenuClicked()


并告诉我是否有任何变化...


-t

Let''s do a first attempt. Try replacing:

AddHandler tsi.Click, AddressOf frmMain.MainMenuClicked()

with

RemoveHandler tsi.Click, AddressOf frmMain.MainMenuClicked()
AddHandler tsi.Click, AddressOf frmMain.MainMenuClicked()

and let me know if anything changes...

-t


这篇关于动态创建的菜单和mdi孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆