如何返回 .Net 中 ContextMenuStrip 中子菜单内 ToolStripMenuItem 的名称
我的问题可能含糊不清,但这是我的情况:
我的表单上有一个方形的图片框数组,每个图片框都有一个处理程序来打开 ContextMenuStrip,其内容是根据目录生成的。目录中的每个文件夹将创建一个 ToolStripMenuItem,并且该文件夹内的每个文件将在所述菜单菜单项的 DropDownItems 内表示。单击菜单的子项后,图片框的图像将根据单击的菜单项而变化。
当我尝试找出单击了哪个子项目时,出现了我的问题。如何通过 ContextMenuStrip 的 _Clicked 事件找到它?到目前为止,这是我的尝试:
private void mstChooseTile_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
ContextMenuStrip s = (ContextMenuStrip)sender;
ToolStripMenuItem x = (ToolStripMenuItem)e.ClickedItem;
// Test to see if I can get the name
MessageBox.Show(x.DropDownItems[1].Name);
// Nope :(
}
My question might be ambiguous but here is my situation :
I have a square array of pictureboxes on my form, each has a handler to open the ContextMenuStrip whose content is generated based on a directory. Each folder in the directory will create a ToolStripMenuItem and each files inside that folder will be represented inside the DropDownItems of said menu menu item. Upon clicking on a sub item of my menu the picturebox's image will change based on what menu item was clicked.
My problem arises when I try to find out which sub item was clicked. How can I find that out with the _Clicked event of the ContextMenuStrip ? Here is my attempt so far :
private void mstChooseTile_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
ContextMenuStrip s = (ContextMenuStrip)sender;
ToolStripMenuItem x = (ToolStripMenuItem)e.ClickedItem;
// Test to see if I can get the name
MessageBox.Show(x.DropDownItems[1].Name);
// Nope :(
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ItemClicked
事件对您不起作用:A) 它仅对直接子级起作用。
B) 即使单击非叶节点也会触发。
尝试订阅每个 ToolStripMenuItem。这里我跳过订阅非叶子节点。
The
ItemClicked
event isn't going to work for you:A) It only works for immediate children.
B) It fires even when clicking non-leaf nodes.
Try subscribing to each ToolStripMenuItem instead. Here I skip subscribing to non-leaf nodes.