2013-03-15 47 views
1

我有一個MenuStrip中包括菜單和工具SubMenu在C#中使用WindowsForm?

在「菜單」

我有一個像msO1,msO2,msO3子菜單......,並在「工具」我有一個像MSP1,MSP2,MSP3子菜單.......,

表格加載所有的子菜單可見是假的......,在按鈕點擊用戶希望選擇他想要的子菜單......,

在文本框中

(txtSelect)如果用戶輸入1,3 ...,他得到msO1,msO3 .....,

我的代碼是一個硬編碼...,如果我有20 subMenus意味着這個代碼是不是有幫助任何人有一個想法。 ...,

private void btnSelect_Click_1(object sender, EventArgs e) 
    { 
     msO1.Visible = false;//msO1 is a submenu 
     msO2.Visible = false; 
     msO3.Visible = false; 
     msP1.Visible = false; 
     msP2.Visible = false; 
     msP3.Visible = false; 
     string word = txtSelect.Text; 
     string[] splt = word.Split(','); 
     int[] arrayItms = new int[splt.Length]; 
     for (int x = 0; x < splt.Length; x++) 
     { 
      arrayItms[x]=Convert.ToInt32(splt[x].ToString()); 
      if (splt.Length > 0) 
      { 
       switch (arrayItms[x]) 
       { 
        case 1: 
         msO1.Visible = true; break; 
        case 2: 
         msO2.Visible = true; break; 
        case 3: 
         msO3.Visible = true; break; 
        case 4: 
         msP1.Visible = true; break; 
        case 5: 
         msP2.Visible = true; break; 
        case 6: 
         msP3.Visible = true; break; 
       } 
      } 
     } 

    } 
+1

你的問題是什麼? – David 2013-03-15 16:18:55

+0

如果我有20個子菜單意味着這個代碼是不是樂於助人的人有一個想法......,因爲實在是太漫長......, – user2173324 2013-03-15 16:20:08

+0

所以,你想使代碼更清潔和更短? – CathalMF 2013-03-15 16:21:19

回答

1

創建您的MenuStrip數組

MenuStrip[] mstrip = new MenuStrip[] 
{ 
    msO1,msO2, msO3, msP1, msP2, msP3 // add other menus here when needed 
}; 

,現在你可以在陣列上工作作爲一個整體,使可見或不可見的菜單

for(int x = 0; x < menus.Length; x++) 
     mstrip[x].Visible = false; 

,你的代碼可以與

簡化
for (int x = 0; x < splt.Length; x++) 
    { 
     int menuIndex; 
     if(Int32.TryParse(splt[x], out menuIndex)) 
     { 
      menuIndex--; 
      if(menuIndex >= 0 && menuIndex < mstrip.Length) 
       mstrip[menuIndex].Visible = true; 
     } 
    } 

記住,數組下標從零開始(當你的用戶可能會開始counti ng a 1)。

+0

史蒂夫乾淨多了知道你已經提供了,但我仍然無法看到的例子將採取如果用戶在TextBox – MethodMan 2013-03-15 16:42:17

+0

我得到它進入'1,3,5'需要可見多個項目的優勢.. ...,你的回答幫助我..., – user2173324 2013-03-15 16:43:16

+0

@Steve你是否缺少這個'string [] splt = txtSelect.Text.Split(',');' – MethodMan 2013-03-15 16:52:20

0

循環遍歷菜單條項中的每個ToolStripMenuItem控件並將它們設置爲可見。 可以附加額外的條件內循環,以確定哪些菜單項應根據用戶的選擇變得可見..

 foreach (ToolStripMenuItem mi in menuStrip1.Items) 
     { 
      mi.Visible = true; 
     } 
+0

這對所有人都重新閱讀他的問題。如果用戶在文本框中輸入1,3,5會怎麼樣?那麼你需要做可見/啓用只有3項 – MethodMan 2013-03-15 16:28:23

+0

用戶可以選擇什麼都子菜單,他要在文本框中.....,基於文本框我想要顯示的子菜單.., – user2173324 2013-03-15 16:28:50

+0

您添加里面的條件循環是否該項目符合用戶請求! – CathalMF 2013-03-15 16:32:25

0

你可以使用類似這樣

string word = txtSelect.Text; 
    string[] splt = word.Split(','); 
    for (int x = 0; x < splt.Length; x++) 
    { 
     Control myControl1 = FindControl("ms" + splt[x]); 
     if (myControl1 != null) 
     (ToolStripMenuItem)myControl1.Visible = true; 
    } 

未經測試,但這種應該讓你在我希望的一半。

+0

什麼的FindControl ... – user2173324 2013-03-15 16:30:58

+0

查找管制形式:http://msdn.microsoft.com/en-gb/library/486wc64h。 ASPX – stevepkr84 2013-03-15 16:33:14