1

我今天向你揭露我的问题。我创建了一个 statusStripLabel 用于显示当前名称 - 当鼠标悬停时,MenuStrip 或 ToolStripMenuItem 的选定项目。我有这个代码:(它正在工作)

private void ouvrirToolStripMenuItem1_MouseHover(object sender, EventArgs e)
{
    ToolStripMenuItem houver = (ToolStripMenuItem)sender;
    MenuStatusLabel.Text = houver.Text;
}

但我想为所有工具条菜单制作,而不仅仅是一个。(一个函数或类似的东西)

4

1 回答 1

1

这适合您的问题和良好的解决方案,我修改了此链接的代码

 private void Form1_Load(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "Ready...";
            foreach (ToolStripMenuItem item in menuStrip1.Items)
            {
                TraverseMenuItemHint(item);
            }
        }

        private void MenuHint_Hint(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = (sender as ToolStripMenuItem).Text;
        }

        private void TraverseMenuItemHint(ToolStripMenuItem element)
        {
            for (int i = 0; i < element.DropDownItems.Count; i++)
            {
                if (!(element.DropDownItems[i] is ToolStripSeparator))
                {
                    ToolStripMenuItem item = element.DropDownItems[i] as ToolStripMenuItem;
                    if (item.Text.Length > 0)
                        item.MouseEnter += MenuHint_Hint;
                    TraverseMenuItemHint(item);
                }
            }
        }

        private void fileToolStripMenuItem_DropDownClosed(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "Ready...";
    }
于 2012-12-28T11:04:23.227 回答