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)
{
}
}
感谢
1.删除项目,'尝试.. catch'块,看看是否有任何异常; 2.使用'System.IO.Path.GetFileName()'方法从完整路径获取文件名。 – max 2012-07-27 02:07:28
谢谢,在删除try&catch之后,在向主菜单添加一个子菜单后,它会出现错误。跨线程调用错误的错误。 LoadTestSuiteMenuStrip()与主GUI不同。但不知道为什么在一个子菜单后抛出这么晚的例外。 – Bopha 2012-07-27 02:14:52
System.IO.Path.GetFileName()非常方便。谢谢! – Bopha 2012-07-27 02:22:36