简体   繁体   English

在运行时添加到条带菜单

[英]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. 好的我有一个字符串列表(实际上是文件名),我想创建一个文件菜单动态表单。

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 ?) 因此,取我的文件名列表,首先是目录字符串和文件sufix的代码条(对于奖金问题,如何将两个删除行包装成一个?)

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); 

So simply cycle through the list and add an item to the "templatesToolStripMenuItem" each time. 因此,只需在列表中循环,每次都将项添加到“templatesToolStripMenuItem”。

but I need to add an event that when the user clicks the item, it sends the file_ varible to the populate.openconfig method. 但是我需要添加一个事件,当用户单击该项时,它会将file_varible发送到populate.openconfig方法。

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. 但我确实可以这样做,因为我将项目添加到菜单栏。

Thank you 谢谢

Aaron 亚伦

So yes in the end i added 所以是的,最后我补充道

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 :) 为帮助的人欢呼,只是喜欢有多少方式可以给猫皮肤:)

If I have understood this correctly, you presumably have this openconfig method which you want to be able to respond to whatever the text is. 如果我已经正确理解了这一点,你可能会有这个openconfig方法,你希望它能够响应任何文本。

The method you pass as the event handler must be of the form void MethodName (object sender, EventArgs e) so you cannot pass it the string directly. 作为事件处理程序传递的方法必须是void MethodName(object sender,EventArgs e)形式,因此您无法直接将字符串传递给它。

However, once your are in your event handle message you can call the relevant message. 但是,一旦您处于事件处理消息中,您就可以调用相关消息。 Eg. 例如。

 subItem.Click += new EventHandler(subItem_Click)
 ...
 void subItem_Click (object sender, EventArgs e)
 {
      ToolStripMenuItem toolstripItem = (ToolStripMenuItem)sender;
      yourObject.openconfig(toolstripItem.Text)
 }

If your object is not avaliable in that scope, you can put your event handler in your object and do the same thing. 如果您的对象在该范围内不可用,您可以将事件处理程序放在对象中并执行相同的操作。

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); 

Then you can access the Tag property from the event handler. 然后,您可以从事件处理程序访问Tag属性。

void subItem_Click (object sender, EventArgs e)
 {
      ToolStripMenuItem toolstripItem = sender as ToolStripMenuItem;

      if (toolstripItem != null && toolstripItem.Tag != null)
      {
          yourObject.openconfig(toolstripItem.Tag.ToString))
      }
 }

One more thing, you could use the Path class for file-path manipulations. 还有一件事,你可以使用Path类进行文件路径操作。 There are bunch of methods to GetFileName, GetFileNameWithoutExtension etc.. GetFileName,GetFileNameWithoutExtension等有很多方法。

string filePath = "C:\diectory\name.txt";
string fileNameWithoutExt = Path.GetFileNameWithoutExtension(filePath);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM