C#拖放DragDrop 无法触发 急!!!

sunny860 2008-11-15 04:35:01
在From内放了 两个控件 listBox1;和Listbox2
在Listbox内增加了几行数据

设置
listBox1.AllowDrop=True
listBox2.AllowDrop=True

代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace DragDrop
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.ListBox listBox2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.listBox2 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(32, 24);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(120, 280);
this.listBox1.TabIndex = 0;
this.listBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.listBox1_MouseDown);
//
// listBox2
//
this.listBox2.ItemHeight = 12;
this.listBox2.Location = new System.Drawing.Point(248, 24);
this.listBox2.Name = "listBox2";
this.listBox2.Size = new System.Drawing.Size(120, 280);
this.listBox2.TabIndex = 0;
this.listBox2.DragDrop += new System.Windows.Forms.DragEventHandler(this.listBox2_DragDrop);
this.listBox2.DragEnter += new System.Windows.Forms.DragEventHandler(this.listBox2_DragEnter);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(408, 333);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.listBox2);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
this.listBox1.AllowDrop = true;
this.listBox2.AllowDrop = true;
this.listBox1.Items.Add("a");
this.listBox1.Items.Add("b");
this.listBox1.Items.Add("c");
}

private void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.listBox1.DoDragDrop(this.listBox1.Items[this.listBox1.SelectedIndex],DragDropEffects.Move);
}

private void listBox2_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
if(e.Data.GetDataPresent("Text"))
{
e.Effect = DragDropEffects.Move;
}
}

private void listBox2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
this.listBox2.Items.Add(e.Data.GetData("Text"));
this.listBox1.Items.Remove(e.Data.GetData("Text"));
}
}
}
运行结果时 Drag的数据可以看到 就是 listBox2_DragDrop()事件,无法触发
请高手指点!!! 急!!
...全文
1011 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengchengwanli 2010-03-31
  • 打赏
  • 举报
回复
顶,怎么解决的?
sunny860 2008-11-19
  • 打赏
  • 举报
回复
listBox无此消息ItemDrag ()
孙晓军82 2008-11-17
  • 打赏
  • 举报
回复
this.listBox2.ItemDrag += new DragEventHandler(listBox2_itemDrag);

private void listBox2_itemDrag(object sender, ItemDragEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
listBox2.DoDragDrop(e.Item, DragDropEffects.Move);
}
}
sunny860 2008-11-17
  • 打赏
  • 举报
回复
问题还不是出在这里啊,
private void listBox2_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
//if(e.Data.GetDataPresent("Text"))
//if(e.Data.GetDataPresent(typeof(object)))
//{
e.Effect = DragDropEffects.Move;//此语句有被执行.我就是注释以上的语句.也不是行啊.
//}
}
目前的问题是listBox2_DragDrop()函数永远无法被触发啊....
ICanUseThisID 2008-11-15
  • 打赏
  • 举报
回复
"Text"不是一个类型


private void listBox2_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
//if(e.Data.GetDataPresent("Text"))
if(e.Data.GetDataPresent(typeof(object)))
{
e.Effect = DragDropEffects.Move;
}
}

private void listBox2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
this.listBox2.Items.Add(e.Data.GetData(typeof(object)));
this.listBox1.Items.Remove(e.Data.GetData(typeof(object)));
}
sunny860 2008-11-15
  • 打赏
  • 举报
回复
顶一下。。有没有帮帮我。。。在此谢过。。
sunny860 2008-11-15
  • 打赏
  • 举报
回复
有设置啊,就是不行。不知道问题出在什么地方。主要是无法触发panel_DragDrop()事件
coolszy 2008-11-15
  • 打赏
  • 举报
回复
属性框里的allowDrop有没有设置
coolszy 2008-11-15
  • 打赏
  • 举报
回复
属性里的 allowDrop 有没有设置

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧