2012-12-28 65 views
1

我今天暴露你的問題。我創建了一個statusStripLabel來顯示當前的名字 - 當鼠標懸停它時,所選擇的MenuStrip或ToolStripMenuItem項。我有這樣的代碼:(它的工作)StatusBar MenuStripItem選擇鼠標

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

但我想爲所有toolstripmenu和沒有一個。 (函數或類似的東西)

+1

http://www.codeproject.com/Tips/254525/Automatically-顯示菜單上,哈弗 – Karthik

+1

http://stackoverflow.com/questions/1919907/set-statusbar-text-on-mouse-hover-works-for-controls-but-not-menuitems – Karthik

+0

我正在讀的兩他們但我不明白! – ShadoWalker

回答

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..."; 
    } 
+0

是的這是工作!感謝您的回答。 – ShadoWalker