在运行时添加到功能区菜单 [英] Adding to strip menu at run time
问题描述
好的,我有一个字符串列表(实际上是文件名),我想创建一个文件菜单动态形式.
OK I have a list of strings (file names in fact), that i want to create a file menu dynamical form.
因此,以我的文件名列表,目录字符串的第一个代码带和文件sufix(有关奖励的问题,我如何将两个删除行合并为一个?)
So taking my list of file names, first code strips of the directory string and file sufix (for bonus question how can I wrap the two remove lines up in to one ?)
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"即可.
So simply cycle through the list and add an item to the "templatesToolStripMenuItem" each time.
但是我需要添加一个事件,当用户单击该项目时,它将file_变量发送到populate.openconfig方法.
but I need to add an event that when the user clicks the item, it sends the file_ varible to the populate.openconfig method.
所以添加项目效果很好,我该如何添加事件处理?
so adding the items works fine, just how do i add the event handling?
我想我可以将其发送给默认方法,该方法在原始数组中搜索完整的文件名,并按照这种方式进行跟踪.但是可以肯定的是,当我将项目添加到菜单栏中时,我可以做到这一点.
I suppose i could send it to a default method that searches for the full file name in the original array and follow it through that way. But surely I can do this as I add the items to the menu bar.
谢谢
亚伦
是的,最后我添加了
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
}
并且刚刚看到我可以如何整理更多的内容:)
and just seen how i can tidy that up a bit more :)
为帮助者们加油,只是喜欢您可以用多种方法给猫咪剥皮:)
Cheers for the help guys, just love how many way you can skin a cat :)
推荐答案
List<string> test_ = populate.Directorylist();
foreach (var file_ in test_)
{
int len_ = file_.Length;
string FullFilename_ = file_.Remove(0, 8);
string filename_ = FullFilename_.Remove(filename_.Length - 4).Trim();
ToolStripItem subItem = new ToolStripMenuItem(filename_);
subItem.Tag = FullFilename;
subItem.Click += new EventHandler(populate.openconfig(file_)); //this is my problem line
templatesToolStripMenuItem.DropDownItems.Add(subItem);
然后,您可以从事件处理程序访问Tag属性.
Then you can access the Tag property from the event handler.
void subItem_Click (object sender, EventArgs e)
{
ToolStripMenuItem toolstripItem = sender as ToolStripMenuItem;
if (toolstripItem != null && toolstripItem.Tag != null)
{
yourObject.openconfig(toolstripItem.Tag.ToString))
}
}
还有一件事情,您可以使用路径用于文件路径操作的类.有很多方法可以使用GetFileName,GetFileNameWithoutExtension等.
One more thing, you could use the Path class for file-path manipulations. There are bunch of methods to GetFileName, GetFileNameWithoutExtension etc..
string filePath = "C:\diectory\name.txt";
string fileNameWithoutExt = Path.GetFileNameWithoutExtension(filePath);
这篇关于在运行时添加到功能区菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!