[英]context menu parent?
Hi I added a context menu on label (c#, winforms). 嗨,我在标签(c#,winforms)上添加了上下文菜单。 my context menu having 3 child items and i want to display label text when i click on any one of context menu items.
我的上下文菜单有3个子项,我想在单击任何一个上下文菜单项时显示标签文本。
thanks in advance 提前致谢
The ContextMenuStrip
control has a SourceControl
property, that will have a reference to the control that opened it. ContextMenuStrip
控件具有SourceControl
属性,该属性将具有对打开它的控件的引用。 You can use that to extract the text from the control: 您可以使用它从控件中提取文本:
private void MenuStripItem_Click(object sender, EventArgs e)
{
ToolStripItem item = (sender as ToolStripItem);
if (item != null)
{
ContextMenuStrip owner = item.Owner as ContextMenuStrip;
if (owner != null)
{
MessageBox.Show(owner.SourceControl.Text);
}
}
}
If you instead of a ContextMenuStrip
use a ContextMenu
, the code should look like this: 如果代替
ContextMenuStrip
使用ContextMenu
,代码应该是这样的:
private void menuItem1_Click(object sender, EventArgs e)
{
MenuItem item = (sender as MenuItem);
if (item != null)
{
ContextMenu owner = item.Parent as ContextMenu;
if (owner != null)
{
MessageBox.Show(owner.SourceControl.Text);
}
}
}
最好的一行:
Control control = ((ContextMenuStrip)((ToolStripItem)sender).Owner).SourceControl;
获取上下文菜单父控件名称MessageBox.Show(contextMenuStrip1.SourceControl.Name.ToString());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.