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 ?)
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.
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.
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 ca开发者_运维技巧n 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);
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))
}
}
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);
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.
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.
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.
精彩评论