0

好的,我有一个字符串列表(实际上是文件名),我想创建一个文件菜单动态表单。

因此,获取我的文件名列表、目录字符串的第一个代码条和文件后缀(对于额外的问题,我如何将两个删除行合并为一个?)

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
        } 

刚刚看到我怎么能把它整理得更清楚一点:)

为帮助的家伙干杯,只是喜欢你可以用多少方法剥猫皮:)

4

2 回答 2

1

如果我正确理解了这一点,您可能拥有这个 openconfig 方法,您希望能够响应任何文本。

作为事件处理程序传递的方法必须采用 void MethodName (object sender, EventArgs e) 形式,因此您不能直接将字符串传递给它。

但是,一旦您在事件句柄消息中,您就可以调用相关消息。例如。

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

如果您的对象在该范围内不可用,您可以将事件处理程序放在您的对象中并执行相同的操作。

于 2011-09-28T22:36:14.197 回答
1
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 属性。

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

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

还有一件事,您可以使用Path类进行文件路径操作。GetFileName、GetFileNameWithoutExtension 等有很多方法。

string filePath = "C:\diectory\name.txt";
string fileNameWithoutExt = Path.GetFileNameWithoutExtension(filePath);
于 2011-09-28T22:56:43.300 回答