How to create a dynamically built Context Menu clickEvent
        Posted  
        
            by 
                Chris
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Chris
        
        
        
        Published on 2012-06-20T13:59:04Z
        Indexed on 
            2012/06/20
            15:16 UTC
        
        
        Read the original article
        Hit count: 306
        
C#, winform
I have a DataGridView 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).
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.
I figured that if I can get the path (let's call it ContextMenuChosen) to show up in MessageBox.Show(ContextMenuChosen); I could set the same to System.Diagnostics.Process.Start(ContextMenuChosen);
The Mydgv_MouseUp event below actually works to the point where I can get it to fire off MessageBox.Show("foo!"); 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...).
I searched code all day yesterday but couldn't find another way to hook up a dynamically built Context Menu clickEvent.
Code and comments:
    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 >= 0 && 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 && 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 > 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();
        }
    }
I'm very close to completing this so any help would be much appreciated.
Thanks,
~ Chris
© Stack Overflow or respective owner