title: How to create a dynamically built Context Menu clickEvent
body: <p>I have a <code>DataGridView</code> and a context menu that opens when you right click a specific column.
  What shows up in the context menu is dependant on what's in the field clicked on - paths to multiple files (the paths are manipulated to create a full UNC path to the correct file).</p>
  
  <p>The only problem is that I can't get the click working.
  I did not drag and drop the context menu from the toolbar, I created it programmically.</p>
  
  <p>I figured that if I can get the path (let's call it <code>ContextMenuChosen</code>) to show up in <code>MessageBox.Show(ContextMenuChosen);</code> I could set the same to <code>System.Diagnostics.Process.Start(ContextMenuChosen);</code></p>
  
  <p>The Mydgv_MouseUp event below actually works to the point where I can get it to fire off <code>MessageBox.Show("foo!");</code> when something in the context menu is selected but that's where it ends. I left in a bunch of comments below showing what I've tried when it one of the paths are clicked. Some result in empty strings, others error (Object not set to an instance...).</p>
  
  <p>I searched code all day yesterday but couldn't find another way to hook up a dynamically built Context Menu clickEvent.</p>
  
  <p>Code and comments:</p>
  
  <pre><code>    ContextMenu m = new ContextMenu();
  
      // SHOW THE RIGHT CLICK MENU
      private void Mydgv_MouseClick(object sender, MouseEventArgs e)
      {
          if (e.Button == MouseButtons.Right)
          {
              int currentMouseOverCol = Mydgv.HitTest(e.X, e.Y).ColumnIndex;
              int currentMouseOverRow = Mydgv.HitTest(e.X, e.Y).RowIndex;
  
              if (currentMouseOverRow &gt;= 0 &amp;&amp; currentMouseOverCol == 6)
              {
                  string[] paths = myPaths.Split(';');
                  foreach (string path in paths)
                  {
                      string UNCPath = "\\\\1.1.1.1\\c$\\MyPath\\";
                      string FilePath = path.Replace("c:\\MyPath\\", @"");
                      m.MenuItems.Add(new MenuItem(UNCPath + FilePath));
                  }
              }
              m.Show(Mydgv, new Point(e.X, e.Y));
          }
      }
  
  
  
      // SELECTING SOMETHING IN THE RIGHT CLICK MENU
      private void Mydgv_MouseUp(object sender, MouseEventArgs e)
      {
          DataGridView.HitTestInfo hitTestInfo;
          if (e.Button == MouseButtons.Right)
          {
              hitTestInfo = Mydgv.HitTest(e.X, e.Y);
              // If column is first column
              if (hitTestInfo.Type == DataGridViewHitTestType.Cell &amp;&amp; hitTestInfo.ColumnIndex == 6)
              {
                  //MessageBox.Show(m.ToString());
                  ////MessageBox.Show(m.Tag.ToString());
                  //MessageBox.Show(m.Name.ToString());
                  //MessageBox.Show(m.MenuItems.ToString());
                  ////MessageBox.Show(m.MdiListItem.ToString());
                  // MessageBox.Show(m.Name);
                  //if (m.MenuItems.Count &gt; 0)
                  //MessageBox.Show(m.MdiListItem.Text);
                  //MessageBox.Show(m.ToString());
                  //MessageBox.Show(m.MenuItems.ToString());
                  //Mydgv.ContextMenu.Show(m.Name.ToString());
                  //MessageBox.Show(ContextMenu.ToString());
                  //MessageBox.Show(ContextMenu.MenuItems.ToString());
                  //MenuItem.text
                  //MessageBox.Show(this.ContextMenu.MenuItems.ToString());
  
              }
  
              m.MenuItems.Clear();
          }
  
      }
  </code></pre>
  
  <p>I'm very close to completing this so any help would be much appreciated.</p>