净ToolStripMenuItem - 如何访问点击事件产生的项目,而正在加载形式(在运行时) [英] .Net ToolStripMenuItem - how to access click-event for items generated while form is being loaded (in runtime)

查看:578
本文介绍了净ToolStripMenuItem - 如何访问点击事件产生的项目,而正在加载形式(在运行时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在它的加载事件中检索数据并填充某些菜单项的形式。我使用的添加菜单项

  Viewer.AudioTrackToolStripMenuItem.DropDownItems.Add(myvalue的)
 

创建的菜单项的数量并不总是相同的。有时他们是2,其他时间他们是4取决于正在检索的数据。

由于菜单项中运行时创建的,我怎么能现在拦截,当用户点击/选择一个菜单项(我的意思是我不知道该控件的名称和它的点击事件)。如果菜单项是前手知,我会简单地创建菜单项在设计时,我有我的点击事件,但现在我不知道。

任何帮助将是AP preciated。谢谢你。

编辑:你让我上赛道,但我还是得到了一些错误信息。只是为了澄清,在code是如下:

  myvalue的= TextBetween(值1,S |,| E)'myvalue的是一个函数的结果
                                          '其中myvalue的是一个字符串
myValue.click + =新System.EventHandler(myValue_Click)错误
Me.AudioTrackToolStripMenuItem.DropDownItems.Add(myvalue的)
 

现在,如果我跑了code,我得到了以下错误:

1)'点击'不是'字符串

中的一员

2)委托System.EventHandler需要AddressOf前pression或λEX pression作为唯一的参数传递给它的构造。

难道上面的错误是因为我的菜单项被命名为根据结果从一个变量(myvalue的)?

EDIT2:我得到的一切使用以下code工作:

 昏暗的菜单项作为ToolStripMenuItem
myvalue的= TextBetween(值1,S |,| E)
菜单项=新ToolStripMenuItem(myvalue的)
Me.AudioTrackToolStripMenuItem.DropDownItems.Add(MenuItem.ToString,没什么,AddressOf ToolStripMenuItem_Click)
 

然后,我增加了以下子:

 私人小组ToolStripMenuItem_Click(BYVAL发件人为System.Object的,BYVALË作为System.EventArgs)
昏暗菜单项作为ToolStripMenuItem = TryCast(发件人,ToolStripMenuItem)
    如果菜单项状态并没有任何然后
          项目刚刚点击。
          的MessageBox.show(menu​​Item.Text)
    结束如果
结束小组
 

解决方案

定义myvalue_click的方法来处理所有myvalue的点击。添加事件编程处理,如:

  this.myValue.Click + =新System.EventHandler(this.myValue_Click);
 

I have a form which upon its loading events retrieves data and populates some menu-items. I am adding menu-items using

Viewer.AudioTrackToolStripMenuItem.DropDownItems.Add(myValue)

The number of menu-items created are not always the same. Sometimes they are 2, other times they are 4 depending on the data being retrieved.

Since the menu-items are created during "run-time", how can I now intercept when a user click/select a menu-item (I mean I don't know the name of the control and its click-event). If the menu-items were known before-hand, I would simply create the menu-item in design-time and I would have my click-event but now I don't.

Any help would be appreciated. Thank you.

Edit: you got me on track but I still got some error-messages. Just to clarify, the code is as follows:

myValue = TextBetween(value1, "s|", "|e") 'myValue is the result of a function
                                          'where myValue is a string
myValue.click += New System.EventHandler(myValue_Click) 'ERROR
Me.AudioTrackToolStripMenuItem.DropDownItems.Add(myValue)

Now if I run the code I get the following errors:

1) 'click' is not a member of 'String'

2) Delegate 'System.EventHandler' requires an 'AddressOf' expression or lambda expression as the only argument to its constructor.

Could above errors be because my menu-items are named in accordance with a result from a variable (myValue)?

edit2: I got everything working by using the following code:

Dim menuItem As ToolStripMenuItem
myValue = TextBetween(value1, "s|", "|e")
menuItem = New ToolStripMenuItem(myValue)
Me.AudioTrackToolStripMenuItem.DropDownItems.Add(MenuItem.ToString, Nothing, AddressOf ToolStripMenuItem_Click)

then I added the following sub:

Private Sub ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim menuItem As ToolStripMenuItem = TryCast(sender, ToolStripMenuItem)
    If menuItem IsNot Nothing Then
          'item just clicked. 
          MessageBox.Show(menuItem.Text)
    End If
End Sub

解决方案

Define myvalue_click method to handle all myValue clicks. Add event handles programmatically, like:

this.myValue.Click += new System.EventHandler(this.myValue_Click);  

这篇关于净ToolStripMenuItem - 如何访问点击事件产生的项目,而正在加载形式(在运行时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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