Domanda

I expose you my problem today. I created a statusStripLabel for showing the current name - the selected item of MenuStrip or ToolStripMenuItem when the mouse is hover it. I have this code : ( it's working )

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

But I want make for all toolstripmenu and no just one. ( a function or something like that )

È stato utile?

Soluzione

This fit from your problem and good solution, I modified the code from this 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...";
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top