2011-09-28 98 views
0

OK我有一個字符串列表(實際上是文件名),我想創建一個文件菜單動態表單。在運行時添加到strip菜單

所以把我的文件名,目錄字符串和文件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); 

所以,簡單地循環通過列表並將每個項目添加到「templatesToolStripMenuItem」。

但我需要添加一個事件,當用戶單擊該項目時,它將file_ varible發送到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 
     } 

和剛剛看到我如何能更是整理了一下:)

乾杯的幫助球員,只是愛你有多少種方式可以皮膚貓:)

+0

希望Path類回答您的獎金問題http://msdn.microsoft.com/en-us/library/system.io.path_methods.aspx – CharithJ

回答

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

很高興看到你的rep sugestes有一個與我曾經擁有過的同一個解決方案:)並且路徑類完美地工作,非常感謝那個提示。 – DevilWAH

1

如果我有understo正確地說,你可能有這個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) 
} 

如果你的對象是不是在該範圍avaliable,你可以把你的事件處理程序在你的對象,做同樣的事情。

+0

OK所以我認爲這是必須做的,在代碼中我有file_ =「diectory \ name.txt」,而filename_ =「名稱」。目前該項目的文本是「名稱」,所以用這種方法,我必須在方法中查找完整的文件名稱,一旦我用事件句柄調用它。只是似乎有點複製,我喜歡。很容易做到,只是想知道我是否可以直接從事件中傳遞完整的文件名。 – DevilWAH

+0

那麼你可以使用lambda表達式,但它本質上是做同樣的事情: 'item.Click + =(o,e)=> {ToolStripMenuItem a =(ToolStripMenuItem)o; A.openconfig(a.Text); };' –

+0

確定簡單,只需使用標記字段並使用完整的文件名即可)。所以是的謝謝你的答案,應該工作得很好:) – DevilWAH