如何确定哪个控件被单击并且 contextMenuStrp 出现?
我已将 contextMenuStrip 分配给两个 ListBox 控件的相同上下文菜单属性。 我想确定其中哪一个激活了上下文菜单。 因为我必须根据运行时单击的控件来更改一些条目。
private void copyNotesToClipboardStripMenu_Click(object sender, EventArgs e)
{
ListBox cntrl = conMenuNotes.SourceControl as ListBox;
//cntrl does not contain info about which ListBox was clicked :((
//check which ListBox was clicked
if (/*oneListBox*/)
{
//do something
}
if(/*anotherLiskBox*/)
{
//do something
}
}
当我阅读另一篇文章时,它应该是 Label 属性中单击的控件的名称。
我看到 cntrl.Label 是 null
我做错了什么? 请告诉我如何解决这种歧义。 谢谢你!
I've assigned a contextMenuStrip to the same contextual menu property of two ListBox controls.
I would like to determine which of them has activated the contextual menu.
Because I have to change some entries depending on which control was clicked at runtime.
private void copyNotesToClipboardStripMenu_Click(object sender, EventArgs e)
{
ListBox cntrl = conMenuNotes.SourceControl as ListBox;
//cntrl does not contain info about which ListBox was clicked :((
//check which ListBox was clicked
if (/*oneListBox*/)
{
//do something
}
if(/*anotherLiskBox*/)
{
//do something
}
}
As i read another posts it should be the name of the clicked control in the Label property.
I see that cntrl.Label is null
What i'm doing wrong ?
Advice me how to resolve this ambiguity.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要将菜单分配给两个控件,而是使用控件上的右键单击事件来执行这样的函数,
完成后也不要忘记将全局设置为
null
并确保使用使用时锁定
。Instead of assigning the menu to the two controls, use the right click event on the controls to execute a function like this
copyNotesToClipboardStripMenu_Click
method you can access the global variable that's storing the clicked controlAlso don't forget to set the global to
null
after you're done with it and to make sure you uselock
when using it.我发现一个对我来说效果很好的结果
controlSelected 对象的属性 Name 包含已激活上下文菜单的控件的名称。
I found a result that works fine for me
The property Name of controlSelected object contains the name of control which has activated the contextual menu.