2012-07-27 59 views
0

我想在運行時將主菜單添加到主菜單中。我看了其他帖子 Adding to strip menu at run time但我不明白我在這裏失蹤,因爲它只填充一個項目,但我有三個xml文件在文件夾中。以下是代碼。測試包參數包含xml文件。C#在運行時填充toolstripmenu項目

public void LoadTestSuiteMenuStrip(string[] testsuite) 
    { 
     try 
     { 
      foreach (var temp in testsuite) 
      { 
       int max = temp.Length-1; 
       while (temp[max] != '\\') 
        max--; 

       max++; 

       //remove the folder path and take only xml file name 
       string name = temp.Substring(max, temp.Length - max); 

       ToolStripMenuItem subItem = new ToolStripMenuItem(name); 
       //subItem.DisplayStyle = ToolStripItemDisplayStyle.Text; 
       //subItem .Text = name; 
       //subItem .Name = name; 
       //subItem.Tag = name; 
       subItem.Click += new EventHandler(testSuiteToolstrip_Click); 
       testsuiteToolStripMenuItem.DropDownItems.Add(subItem); 
      } 
     } 
     catch (Exception error) 
     { 
     } 
    } 

感謝

+0

1.刪除項目,'嘗試.. catch'塊,看看是否有任何異常; 2.使用'System.IO.Path.GetFileName()'方法從完整路徑獲取文件名。 – max 2012-07-27 02:07:28

+0

謝謝,在刪除try&catch之後,在向主菜單添加一個子菜單後,它會出現錯誤。跨線程調用錯誤的錯誤。 LoadTestSuiteMenuStrip()與主GUI不同。但不知道爲什麼在一個子菜單後拋出這麼晚的例外。 – Bopha 2012-07-27 02:14:52

+0

System.IO.Path.GetFileName()非常方便。謝謝! – Bopha 2012-07-27 02:22:36

回答

0

確保您從主線程訪問GUI。如果你想從另一個線程增加而不是

LoadTestSuiteMenuStrip(args); 

使用

Invoke(new Action<string[]>(LoadTestSuiteMenuStrip), new object[] { args }); 
+0

現在它的工作表示感謝。 – Bopha 2012-07-27 02:40:39