2015-10-22 45 views
0

我是新來的人,希望能做得好。如何在合併的菜單條上實施MouseEnter事件

我正在一個文本編輯器(c#)中,我有一個父親的形式,我可以創建幾個孩子的形式。當我打開一個兒童窗體時,他的menuStrip將他父親的菜單條合併爲白色,並且我具有父級menuStrip上的子菜單的所有功能(如某些ToolStripMenuItems)。 我的問題是:我想爲這些ToolStripMenuItems實現MouseEnter事件。此事件將更改toolStripStatusLabel(僅位於父表單中)上的文本,並在其上寫入啓動事件的ToolStripMenuItems的名稱。我可以在父親的菜單條的所有ToolStripMenuItems上輕鬆完成,但是我不知道該怎麼做,才能爲Child's menutrip的所有ToolStripMenuItems啓動此事件。我希望他們也改變ToolStripStatusLabel。

public void ratonencima(object sender, EventArgs e) 
    { 
     ToolStripMenuItem aux = (ToolStripMenuItem)sender; 
     this.toolStripStatusLabel1.Text = aux.Text; 
    } 

這是我從父窗體的ToolStripMenuItem管理MouseEnter事件時調用的方法。我想將事件添加到ToolStripMenuItem中(在執行時),因爲在我創建子表單時他的問題沒有問題,並且他的MenuStrip合併了父類。

我該怎麼辦?

+0

只要不要以爲合併是相關的。您的孩子表單在獲得MouseEnter事件時應該觸發一個事件。您的主窗體可以訂閱它並更新其狀態欄。 –

回答

0

那麼,最後我找到了解決我的問題的方法。 我動態地將MouseEnter事件添加到ToolStripMenuIitems(並且我還向該ToolStripMenuItems添加了一個MouseLeave事件,以便在文本檢測到事件時更改爲空字符串)。

我加了父親的形式對這些方法:

private void eventoaelementos() 
{ 
foreach(ToolStripMenuItem t in this.menuStrip1.Items) 
    { 
    t.MouseEnter += new EventHandler(metodoMouseEnter); 
    t.MouseLeave += new EventHandler(metodoMouseLeave); 
    foreach(ToolStripItem t2 in t.DropDownItems) 
    { 
     if(t2 is ToolStripMenuItem) 
     { 
     //To discard the ToolStripSeparator's case in which I 
     //obtain an exception 
      t2.MouseEnter += new EventHandler(metodoMouseEnter); 
      t2.MouseLeave += new EventHandler(metodoMouseLeave); 
     } 
    } 
    } 
} 

private void metodoMouseLeave(object sender, EventArgs e) 
    { 
     this.toolStripStatusLabel1.Text = ""; 
    } 

    private void metodoMouseEnter(object sender, EventArgs e) 
    { 
     ToolStripItem t = sender as ToolStripItem; 
     if(t!=null) 
     { 
      this.toolStripStatusLabel1.Text = t.Text; 
     } 
    } 

,一旦我初始化父親的表格(在構造函數),當我創建一個孩子的表格(我有我打電話到eventoaelementos的方法「父」形式的MenuStrip上的「新建」按鈕,當我單擊此按鈕時,我在調用的方法上添加了此方法)。

我希望你覺得這個很有用。

謝謝你的時間。