2014-06-23 59 views
-3

我想让用户拖动任何项目从MenuStrip到列表框。 我之间做了ListBoxes,但不能用MenuStrip。 非常感谢您的帮助。C# - 拖动MenuStrip项目到列表框

我使用的WinForms,C#

对于目的地列表框我修改其属性 this.listBox2.AllowDrop = true; 并创建了以下两个事件:

private void listBox2_DragOver(
object sender, System.Windows.Forms.DragEventArgs e) 
{ 
e.Effect=DragDropEffects.All; 
} 


private void listBox2_DragDrop(
object sender, System.Windows.Forms.DragEventArgs e) 
{ 
if(e.Data.GetDataPresent(DataFormats.StringFormat)) 
{ 
    string str= (string)e.Data.GetData(
     DataFormats.StringFormat);    
    listBox2.Items.Add(str); 
} 
} 

我需要的是什么应该做源的MenuStrip允许从ListBox中拖拽项目,以及如何使MenuStrip可拖拽。

感谢大家的帮助。

+0

您可以发布您的代码吗?人们更容易指出你出错的地方。 – Steve

+0

UI的技术是什么?的WinForms? WPF?您需要向我们展示一些代码 – Tomtom

+0

我使用WinForms,C# –

回答

0

拖拽菜单项条是同样喜欢ListBox项目。 检查您的代码...

1

我找到了解决办法: 丢失的事件是,我要补充事件ToolStripMenuItem_MouseDown,我更喜欢用右键点击,而不是左击,以避免ToolStripMenuItem_Click和拖动事件之间的矛盾,这个代码:

  AllowDrop = true; 

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

还添加以下代码到ListView:

private void lvAllowDropListView_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) 
    { 
     System.Windows.Forms.ToolStripMenuItem button = e.Data.GetData(typeof(System.Windows.Forms.ToolStripMenuItem)) 
         as System.Windows.Forms.ToolStripMenuItem; 
     if (button != null) 
      { 
     try 
     { 
      SmallImageList = sysIcons.SmallIconsImageList; 
      LargeImageList = sysIcons.LargeIconsImageList; 

      System.Windows.Forms.ToolStripMenuItem item = e.Data.GetData(typeof(System.Windows.Forms.ToolStripMenuItem)) 
         as System.Windows.Forms.ToolStripMenuItem; 
      if (item != null) 
      { 
       AddToolStripMenuItem(item.Text, item.Name); 
      } 
     } 
     catch { } 
      } 
    } 
    private void AddToolStripMenuItem(string name, string tag) 
    { 
     System.Windows.Forms.ListViewItem item = new System.Windows.Forms.ListViewItem(name); 
     int Index = -1; 
     for (int i = 0; i < Items.Count;i++) 
      if(Items[i].Tag.ToString() == tag) 
      { 
       Index = i; 
       break; 
      } 
      if (Index == -1) 
      { 
       item.Tag = tag; 
       Items.Add(item); 
      } 
    }