2013-02-03 48 views
0

我該如何使用這一行命令在Form main中檢查一個菜單項?如何使用此命令行來檢查菜單項?

//Form main: 

    private void button1_Click_1(object sender, EventArgs e) 
    { 
     Form1 form2 = new Form1(); 
     form2.Owner = (Form)this; 
     form2.Show(); 

    } 




Form1: 

    private void button1_Click(object sender, EventArgs e) 
    { 


     this.Owner.MainMenuStrip.Items.Find("exit", true).First().Text = "test"; 


    } 

我可以更改文本或啓用或禁用或...但我不能把它的Click事件(有了這個代碼)或改變其checkstate

回答

0

您需要的項目來投一個ToolStripMenuItem,因爲Find方法返回ToolStripItem類型的項目(ToolStripMenuItem繼承自)。

實施例:

private void FindItAndCheck() 
{ 
    ToolStripMenuItem item = 
     this.MainMenuStrip.Items.Find("exit", true).FirstOrDefault() 
     as ToolStripMenuItem; 

    if (item == null) throw new ApplicationException("..."); 
    else 
    { 
     item.Checked = true; 
    } 
} 
+0

由於親愛Steinar – user2037717