有没有办法使 ToolStripMenuItem 在菜单和工具栏之间共享?

发布于 2024-10-04 20:05:34 字数 471 浏览 7 评论 0原文

我正在使用 Winforms 将旧的 VB6 项目移植到 C#。原始版本使用 DataDynamic 的 ActiveBar 套件来同时管理工具栏、菜单和快捷功能区中多个项目的更改。要更改在多个菜单、工具栏和上下文菜单之间共享的项目的文本,所有旧代码所要做的就是更改“工具”对象的“标题”属性。

我觉得 .NET 必须有某种方式在各种容器之间共享 ToolStripItem,但看起来并非如此。我错过了什么吗?

目前,我已经多次开始提出各种想法,从在 ToolStripMenuItem 对象上实现 Clone 扩展方法,到让我的表单跟踪每个可共享的内容。当我意识到更新本地副本不会更新其他副本时,这失败了(我必须添加复杂的更新事件,这比简单地手动打开和关闭每个项目要工作更多)。我考虑过创建某种方法来根据其标签更新所有项目。这似乎也是不太可行的。

你们在类似情况下都使用过哪些技巧?

I'm porting an old VB6 project to C#, using Winforms. The original uses DataDynamic's ActiveBar suite to manage changes to multiple items in toolbars, menus, and shortcut ribbons simultaneously. All the old code has to do to change the text of an item shared between several menus, a toolbar, and a context menu is change the "tool" object's "Caption" property.

I feel like .NET must have some way of sharing ToolStripItems between a variety of containers, but it doesn't look like it does. Am I missing something?

Currently I've started several times with a variety of ideas, from implementing a Clone extension method on ToolStripMenuItem objects and having my form keep track of each sharable. That failed when I realized that updating the local copy wouldn't update the others (I'd have to add complex updating events that is more work than simply turning each item on and off by hand). I've considered creating some way of updating all items based on its tag. That seemed pretty infeasible as well.

What techniques have y'all used in similar situations?

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

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

发布评论

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

评论(2

如果没结果 2024-10-11 20:05:34

嗯,这实际上是 .Net GUI 构建库的烦人限制之一。

常见的解决方法是动态管理菜单,在菜单打开时向其添加项目。从用户体验的角度来看,虽然丑陋,但相当无缝。
诀窍是使用以下 UI 设计范式:菜单永远不会同时显示,因此您可以热交换项目

private void myContextMenuStrip_Opening(object sender, CancelEventArgs e)
{
    myContextMenuStrip.Items.Insert(3, myToolStripMenuItem);
}  

然后使用标志和代码逻辑,您将能够知道,当菜单/工具栏/上下文菜单打开时< /em>,它应该显示哪些项目。

Well, this is actually one of the annoying limitations of .Net GUI-Building Libraries

A common workaround would be managing your Menus dynamically, adding Items to them as they are opening. Ugly but quite seamless from the user experience point of view.
The trick is to use the following UI design pradigm : Menus are never displayed simultaneously, so you can hot-swap Items

private void myContextMenuStrip_Opening(object sender, CancelEventArgs e)
{
    myContextMenuStrip.Items.Insert(3, myToolStripMenuItem);
}  

Then using flags and code logic you would be able to know, As the Menu/ToolBar/ContextMenu is opened, what Items it should display.

々眼睛长脚气 2024-10-11 20:05:34

我最终实现了一个 SharedMenuItem ,它的职责是注册和更新需要更改的 UI 元素。上下文菜单的差异足以保证有自己的处理程序。

我没有更新所有 MenuItem,而是更新了 SharedMenuItem,然后它负责其他更改。

[编辑] 代码(添加其他属性很简单/甚至可以自动生成):

public class SharedMenuItem
{
    bool enabled = false;
    public bool Enabled
    {
        get { return enabled; }
        set
        {
            enabled = value;
            foreach (var member in members)
            {
                member.Enabled = value;
            }
        }
    }

    bool visible;
    public bool Visible
    {
        get { return visible; }
        set
        {
            visible = value;
            foreach (var member in members)
            {
                member.Visible = visible;
            }
        }
    }

    string text;
    public string Text
    {
        get { return text; }
        set
        {
            text = value;
            foreach (var member in members)
            {
                member.Text = text;
            }
        }
    }

    string toolTipText = "";
    public string ToolTipText
    {
        get { return toolTipText; }
        set
        {
            toolTipText = value;
            foreach (var member in members)
            {
                member.ToolTipText = toolTipText;
            }
        }
    }

    private List<ToolStripItem> members = new List<ToolStripItem>();

    public void Remove(ToolStripItem member)
    {
        members.Remove(member);
    }

    public void Add(ToolStripItem member)
    {
        members.Add(member);
    }

    public SharedMenuItem(string text)
    {
        this.text = text;
    }
}

I wound up implementing a SharedMenuItem that's responsibility was to register and update the UI elements that needed change. The context menu wound up being different enough to warrant its own handler.

Instead of updating all the MenuItems I updated the SharedMenuItem which then took care of the other changes.

[EDIT] The code (adding other properties is trivial/automatically-generatable even):

public class SharedMenuItem
{
    bool enabled = false;
    public bool Enabled
    {
        get { return enabled; }
        set
        {
            enabled = value;
            foreach (var member in members)
            {
                member.Enabled = value;
            }
        }
    }

    bool visible;
    public bool Visible
    {
        get { return visible; }
        set
        {
            visible = value;
            foreach (var member in members)
            {
                member.Visible = visible;
            }
        }
    }

    string text;
    public string Text
    {
        get { return text; }
        set
        {
            text = value;
            foreach (var member in members)
            {
                member.Text = text;
            }
        }
    }

    string toolTipText = "";
    public string ToolTipText
    {
        get { return toolTipText; }
        set
        {
            toolTipText = value;
            foreach (var member in members)
            {
                member.ToolTipText = toolTipText;
            }
        }
    }

    private List<ToolStripItem> members = new List<ToolStripItem>();

    public void Remove(ToolStripItem member)
    {
        members.Remove(member);
    }

    public void Add(ToolStripItem member)
    {
        members.Add(member);
    }

    public SharedMenuItem(string text)
    {
        this.text = text;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文