【发布时间】:2011-11-27 06:30:22
【问题描述】:
好的,我有一个字符串列表(实际上是文件名),我想创建一个文件菜单动态表单。
因此,获取我的文件名列表,目录字符串和文件后缀的第一个代码条(对于额外的问题,我如何将两个删除行合并为一个?)
List<string> test_ = populate.Directorylist();
foreach (var file_ in test_)
{
int len_ = file_.Length;
string filename_ = file_.Remove(0, 8);
filename_ = filename_.Remove(filename_.Length - 4).Trim();
ToolStripItem subItem = new ToolStripMenuItem(filename_);
subItem.Click += new EventHandler(populate.openconfig(file_)); //this is my problem line
templatesToolStripMenuItem.DropDownItems.Add(subItem);
因此,只需在列表中循环并每次将一个项目添加到“templatesToolStripMenuItem”。
但我需要添加一个事件,当用户单击该项目时,它将 file_ 变量发送到 populate.openconfig 方法。
所以添加项目工作正常,我该如何添加事件处理?
我想我可以将它发送到一个默认方法,该方法在原始数组中搜索完整文件名并按照这种方式进行操作。但是当我将项目添加到菜单栏时,我当然可以做到这一点。
谢谢
亚伦
所以是的,最后我添加了
subItem.tag = File_
....
then have the event handle to
void subItem_Click (object sender, EventArgs e) //open files from menu
{
ToolStripMenuItem toolstripItem = (ToolStripMenuItem)sender;
string filename_ = toolstripItem.Tag.ToString(); //use the tag field
populate.openconfig(filename_);
populate.Split(_arrayLists); //pass read varible dictonary to populate class to further splitting in to sections.
Populatetitle();//Next we need to populate the Titles fields and datagrid view that users will enter in the Values
}
刚刚看到我可以多整理一下:)
为帮助的家伙干杯,只是喜欢你可以用多少方法给猫剥皮 :)
【问题讨论】:
-
Hope Path 班回答你的奖金问题msdn.microsoft.com/en-us/library/system.io.path_methods.aspx
标签: c# menu toolstripmenu