0

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 can tidy that up a bit more :)

Cheers for the help guys, just love how many way you can skin a cat :)

Soheil Farahani
  • 349
  • 1
  • 2
  • 13
DevilWAH
  • 2,553
  • 13
  • 41
  • 57
  • Hope Path class answer your bonus question http://msdn.microsoft.com/en-us/library/system.io.path_methods.aspx – CharithJ Sep 28 '11 at 23:01

2 Answers2

1

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.

T. Kiley
  • 2,752
  • 21
  • 30
  • OK so that how i was thinking it would have to be done, in the code i have file_ = "diectory\name.txt" while filename_ = "name". currently the text of the item is the "name", so with this method, i have to look up the full file name in method once I have called it with the event handle. just seemed a bit to much replication for my liking. easy enough to do, just wondered if I could pass the full file name directly from the event. – DevilWAH Sep 28 '11 at 22:44
  • Well you can use lambda expressions but it is essentially doing the same thing: `item.Click += (o, e) => { ToolStripMenuItem a = (ToolStripMenuItem)o; A.openconfig(a.Text); };` – T. Kiley Sep 28 '11 at 22:53
  • OK simple just use the tag field and use the full file name for that ;). So yes thanks for the answer, should work just great :) – DevilWAH Sep 28 '11 at 22:54
  • You could, for a bit more work, create a class that inherits from ToolStripMenuItem that also has a string that stores the full file name and then access it that way? – T. Kiley Sep 28 '11 at 22:58
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); 

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);
CharithJ
  • 46,289
  • 20
  • 116
  • 131
  • Nice to see some one with your rep sugestes the same solution i had hased togather :) And path class works perfectly thanks for that tip. – DevilWAH Sep 28 '11 at 23:35