0

Say, for example, I have a ToolStripMenu. I have a tab already made (let's call it Download), and want a substrip of it (the "type here" part) to be automaticaly made. I want it to be text that the string downloadedString is. Then, later, when it's clicked, I want it to fire:

Process.Start("google.com/" + Text of the substrip clicked);

How do I do this?

Jon
  • 2,566
  • 6
  • 32
  • 52

1 Answers1

2

You can do so via the Click event handler on the ToolStripMenuItem.

Part 1 - programmatically adding menu items

Just add a new ToolStripMenuItem to the MenuStrip like so:

ToolStripMenuItem mi = new ToolStripMenuItem("whatever");
mi.Click += new EventHandler(menuItemHandler_Click);

menuStrip1.Items.Add(mi);

They can all reference the same event handler (see below).

Part 2 - event handler to start your process

The event handler will start the process, using the text of the menu item that was clicked:

private void menuItemHandler_Click(object sender, EventArgs e)
{
    Process.Start("google.com/" + (sender as ToolStripMenuItem).Text);
}

Based on the code above, Process.Start() will receive google.com/whatever as the parameter.

Brendan Green
  • 11,676
  • 5
  • 44
  • 76
  • That doesn't add it as a part of Downloads. It does, however, add a blank space, like [this](http://puu.sh/18FPO). Any ideas why? – Jon Sep 26 '12 at 01:17
  • Because the example code adds the ToolStripMenuItem to the "root" of the MenuStrip. If you want to add it to your existing Downloads menu, then add the new ToolStripMenuItem to the DropDownItems collection of your Downloads menu. – Brendan Green Sep 26 '12 at 01:42
  • Ok, I got it to show up as a child of Downloads. It errors out (like last time) with the error "The system cannot find the file specified" – Jon Sep 26 '12 at 01:47
  • That only happens when I click by the way. My code is [here](http://pastebin.com/MTZDgL4a) – Jon Sep 26 '12 at 01:48
  • That's probably because when you call **Process.Start** it is looking for a file named "google.com". I suspect that you want to actually start "http://google.com". – Brendan Green Sep 26 '12 at 01:52
  • Yeah, I've done it before... Edit: *facepalm* Just realized. I need the Http://. – Jon Sep 26 '12 at 01:53