将ToolStripMenuItem拖放到ListView

时间:2018-05-17 10:34:31

标签: c# winforms

在c#windows应用程序中使用Menustrip和ToolStripMenuItem。粘贴了我的代码。我想要实现的是能够右键单击并将菜单项拖动到列表视图中。

此外,当在列表视图中选择该项目时,我需要访问菜单项属性。

这是我上述场景的代码,在这里我需要帮助。

当在ToolStripItem的MouseDown事件上调用DoDragDrop方法时,此时'sender'是ToolStripItem对象,但是当列表视图上出现DragDrop时,'sender'显然变为ListViewItem。

有什么方法可以在ListView dragdrop事件中获取ToolStripItem对象引用。

请指导。

using System;
using System.Collections.Generic; 
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace testmenudrag
{
    public partial class Form1 : Form
    {
        MenuStrip mmenu = new MenuStrip();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ToolStripMenuItem mnulevel1 = new ToolStripMenuItem();
            mnulevel1.Text = "First Level";

            ToolStripMenuItem mnulevel2 = new ToolStripMenuItem();
            mnulevel2.Text = "Second Level";

            ToolStripMenuItem mnulevel3 = new ToolStripMenuItem();
            mnulevel3.Text = "Third Level";
            mnulevel3.Tag = 3;
            mnulevel3.MouseDown += mnulevel3_MouseDown;

            mnulevel2.DropDown.Items.Add(mnulevel3);
            mnulevel1.DropDown.Items.Add(mnulevel2);
            mmenu.Items.Add(mnulevel1);
            this.Controls.Add(mmenu);
        }

        void mnulevel3_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                ToolStripMenuItem mnusel = (ToolStripMenuItem)sender;
                mnusel.DoDragDrop(sender, DragDropEffects.Copy);
            }
        }

        private void listView1_DragDrop(object sender, DragEventArgs e)
        {
            try
            {
                // The below code generates a cast error since here
                // Object sender is a listviewitem
                ToolStripMenuItem mmnu = (ToolStripMenuItem)sender;

                 // Tried the below code too, in this case, mmnu is null 
                 ToolStripMenuItem mmnu =                                     
                e.Data.GetData(typeof(ToolStripMenuItem)) as 
                 ToolStripMenuItem;

                String menuitemstr = mmnu.Text;
                ListViewItem lv = new ListViewItem(menuitemstr);
                listView1.Items.Add(lv);
            }
            catch
            {
            }
        }

        private void listView1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

出于某种原因,在拖放操作期间ToolStripMenuItem会投放到ToolStripItem。以下代码更新有效:

void mnulevel3_MouseDown(object sender, MouseEventArgs e)
{
  if (e.Button == System.Windows.Forms.MouseButtons.Right)
  {
    ToolStripMenuItem mnusel = (ToolStripMenuItem)sender;
    mnusel.DoDragDrop(mnusel, DragDropEffects.Copy);
  }
}

private void listView1_DragDrop(object sender, DragEventArgs e)
{
  try
  {
    // see the contained types
    var formats = e.Data.GetFormats();
    Array.ForEach(formats, item => Debug.WriteLine("Supported format: " + item.ToString()));

    ToolStripItem mmnu = e.Data.GetData(typeof(ToolStripItem)) as ToolStripItem;

    String menuitemstr = mmnu.Text;
    ListViewItem lv = new ListViewItem(menuitemstr);
    listView1.Items.Add(lv);
  }
  catch(Exception ex)
  {
    Debug.WriteLine("listView1_DragDrop exception: " + ex);
  }
}