c#如何从至少两个级别的基础子菜单中转到上下文menustrip

发布于 2025-02-01 18:37:48 字数 287 浏览 1 评论 0 原文

如何从其基础子菜单中转到ContextMenustrip,它是工具stripdropdownmenu的工具速率,它是该contextMenustrip的toolstripmenuitem的下拉?

问题在于工具条流式曼努犬类,在该类中找不到属性,请参阅上级对象,例如其cassived toolstripmenuitem.its parent,容器属性和getContainerControl()方法all return null。

我的目的是在单击任何基础菜单项时都知道ContextMenustrip的SourceControl。

How to get the referece to ContextMenuStrip from its underlying sub menuitem which is a ToolStripMenuItem of a ToolStripDropDownMenu which is a Dropdown of a ToolstripMenuItem of this ContextMenuStrip?

the problem lies in the ToolStripDropDownMenu class,where can't find a property refer to upperlevel objects such as its assciated ToolStripMenuItem.Its Parent,Container properties and GetContainerControl() method all return null.

My purpose is to know the SourceControl of the ContextMenuStrip when any of its underlying menu items are clicked.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尐偏执 2025-02-08 18:37:48

您可以使用 onalyItem 属性获取 toolstripitem

private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    var comboBox = sender as ToolStripComboBox;
    var twoLevelParent = comboBox.OwnerItem.OwnerItem;
}

You can use OwnerItem property to get the ToolStripItem:

private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    var comboBox = sender as ToolStripComboBox;
    var twoLevelParent = comboBox.OwnerItem.OwnerItem;
}
‘画卷フ 2025-02-08 18:37:48

您需要向后循环才能获得第一个 asloreTem 给定 toolsstripitem 的属性(或 sunived ofer noreflow noreferrer“> suret oferived 类) toolstrip (可以是 toolstrip menustrip statusstrip ,或 condect> contextmenustrip 控制)通过 toolstripitem.outher 属性。

考虑以下扩展方法:

public static class ToolStripExtensions
{
    public static ToolStrip GetMainToolStrip(this ToolStripItem tsi)
    {
        if (tsi == null) return null;
        if (tsi.OwnerItem == null) return tsi.Owner;

        var oi = tsi.OwnerItem;

        while (oi.OwnerItem != null) oi = oi.OwnerItem;

        return oi.Owner;
    }

    public static ToolStrip GetMainToolStrip(this ToolStrip ts)
    {
        ToolStrip res = ts;

        var m = ts as ToolStripDropDownMenu;

        if (m?.OwnerItem != null)
            res = GetMainToolStrip(m.OwnerItem);

        return res;
    }
}

以下是一个示例,该示例显示了如何获得 contextMenustrip.sourcecontrol itemClicked 事件:事件:

private void cmnu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
    if (e.ClickedItem.GetMainToolStrip() is ContextMenuStrip cmnu)
        Console.WriteLine(cmnu.SourceControl.Name);
}

You need a backward loop to get the first OwnerItem property of a given ToolStripItem (or one of the derived classes) in a branch then you can get the main ToolStrip (which can be a ToolStrip, MenuStrip, StatusStrip, or ContextMenuStrip control) through the ToolStripItem.Owner property.

Consider the following extension methods:

public static class ToolStripExtensions
{
    public static ToolStrip GetMainToolStrip(this ToolStripItem tsi)
    {
        if (tsi == null) return null;
        if (tsi.OwnerItem == null) return tsi.Owner;

        var oi = tsi.OwnerItem;

        while (oi.OwnerItem != null) oi = oi.OwnerItem;

        return oi.Owner;
    }

    public static ToolStrip GetMainToolStrip(this ToolStrip ts)
    {
        ToolStrip res = ts;

        var m = ts as ToolStripDropDownMenu;

        if (m?.OwnerItem != null)
            res = GetMainToolStrip(m.OwnerItem);

        return res;
    }
}

Here's an example that shows how to get the ContextMenuStrip.SourceControl in the ItemClicked event:

private void cmnu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
    if (e.ClickedItem.GetMainToolStrip() is ContextMenuStrip cmnu)
        Console.WriteLine(cmnu.SourceControl.Name);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文