如何创建动态构建的上下文菜单 clickEvent [英] How to create a dynamically built Context Menu clickEvent

查看:18
本文介绍了如何创建动态构建的上下文菜单 clickEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataGridView 和一个上下文菜单,当您右键单击特定列时会打开该上下文菜单.上下文菜单中显示的内容取决于单击的字段中的内容 - 多个文件的路径(操作路径以创建正确文件的完整 UNC 路径).

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.

我想如果我能得到路径(我们称之为 ContextMenuChosen)以显示在 MessageBox.Show(ContextMenuChosen); 我可以将相同的设置为 <代码>System.Diagnostics.Process.Start(ContextMenuChosen);

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);

下面的 Mydgv_MouseUp 事件实际上工作到我可以让它触发 MessageBox.Show("foo!"); 当上下文菜单中的某些内容被选中但它结束的地方.我在下面留下了一堆评论,显示了单击其中一个路径时我尝试过的内容.一些导致空字符串,其他错误(对象未设置为实例...).

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...).

我昨天一整天都在搜索代码,但找不到其他方法来连接动态构建的上下文菜单 clickEvent.

I searched code all day yesterday but couldn't find another way to hook up a dynamically built Context Menu clickEvent.

代码和注释:

    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.

推荐答案

我没有看到连接到您的菜单项的事件处理程序,因此单击它时会发生某些事情:

I don't see an event handler wired to your menu item, so that something will happen when you click it:

void menu_Click(object sender, EventArgs e) {
  MessageBox.Show(((MenuItem)sender).Text);
}

然后在将菜单项添加到上下文菜单时附加它:

Then attach it when you add the menu item to the context menu:

  MenuItem mi = new MenuItem(UNCPath + FilePath);
  mi.Click += menu_Click;
  m.MenuItems.Add(mi);

这篇关于如何创建动态构建的上下文菜单 clickEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆