StatusBar MenuStripItem选择鼠标

时间:2012-12-28 04:47:35

标签: c# menustrip statusstrip toolstripmenu

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

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

但是我想为所有的工具线程设置制作,而不是只有一个。 (一个函数或类似的东西)

1 个答案:

答案 0 :(得分:1)

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

的代码
 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...";
    }