简体   繁体   中英

Drag drop ToolStripMenuItem into ListView

Working with Menustrip and ToolStripMenuItem in c# windows application. Have pasted my code below. What I'm trying to achieve is to be able to right click and drag a menu item into the listview.

Further when that item is selected in the listview I need to access the menu item properties.

This is my code for the above scenario and here I need a help.

When the DoDragDrop method is called on the ToolStripItem's MouseDown event, at that point the 'sender' is a ToolStripItem Object, but when the DragDrop occurs on the listview the 'sender' becomes a ListViewItem apparently.

Is there any way I can get the ToolStripItem object reference in the ListView dragdrop event.

Please guide.

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

For some reason, the ToolStripMenuItem is casted to ToolStripItem during the drag/drop operation. The following update of your code works:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM