private void listBox1_MouseDown(object sender, MouseEventArgs e)
{ if(e.Button == System.Windows.Forms.MouseButtons.Left)
(sender as ListBox).DoDragDrop((sender as ListBox).SelectedItem, DragDropEffects.Move);
}
ListBox listbox = null;
string s = "";
private void listBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(ListBox)) && e.Data.GetDataPresent(DataFormats.Text))
{
listbox = e.Data.GetData(typeof(ListBox)) as ListBox;
s = e.Data.GetData(DataFormats.Text).ToString();
e.Effect = DragDropEffects.Move;
}
}
private void listBox1_DragDrop(object sender, DragEventArgs e)
{
(sender as ListBox).Items.Add(s);
listbox.Items.Remove(s);
}上面代码,实现listBox1和listBox2的相互拖动项,都注册了上面那些事件,
为什么不成功呢,哪里错了呢?
谢谢!!
{ if(e.Button == System.Windows.Forms.MouseButtons.Left)
(sender as ListBox).DoDragDrop((sender as ListBox).SelectedItem, DragDropEffects.Move);
}
ListBox listbox = null;
string s = "";
private void listBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(ListBox)) && e.Data.GetDataPresent(DataFormats.Text))
{
listbox = e.Data.GetData(typeof(ListBox)) as ListBox;
s = e.Data.GetData(DataFormats.Text).ToString();
e.Effect = DragDropEffects.Move;
}
}
private void listBox1_DragDrop(object sender, DragEventArgs e)
{
(sender as ListBox).Items.Add(s);
listbox.Items.Remove(s);
}上面代码,实现listBox1和listBox2的相互拖动项,都注册了上面那些事件,
为什么不成功呢,哪里错了呢?
谢谢!!
解决方案 »
- 如何删除一个文件的时候,删除另外的一个文件
- 求,Winform TreeView延迟事件设计思路
- 关于DataTable.Select(),DataRow[],DataTable....
- 请教一高难度的函数转换
- 编写一个下拉列表框(combox)控件,支持基本下拉列表框控件的基本功能,并在下拉框上添加“清空列表”功能和下拉框拖拉尺寸功能
- C#窗体布局,菜单栏工具栏和Pannel的布局问题
- 求助:C#写多节点xml
- c#中不明白的地方。。
- c# 蓝牙编程 bluetoothclient的使用
- 如何通过函数实现所有子窗体的新建与激活。
- 刚学C#,帮我看看这个程序错在哪里?
- 紧急求助:控件被遮盖问题
private bool isLeftMouseButtonDown = false; private void listBox_DragDrop(object sender, DragEventArgs e)
{
ListBox destListBox = sender as ListBox;
if (e.Data.GetDataPresent(typeof(ListBox)))
{
ListBox srcListBox = e.Data.GetData(typeof(ListBox)) as ListBox;
destListBox.Items.Add(srcListBox.SelectedItem);
srcListBox.Items.Remove(srcListBox.SelectedItem);
}
} private void listBox_DragEnter(object sender, DragEventArgs e)
{
e.Effect = e.AllowedEffect;
} private void listBox_MouseDown(object sender, MouseEventArgs e)
{
isLeftMouseButtonDown = true;
} private void listBox_MouseMove(object sender, MouseEventArgs e)
{
if (isLeftMouseButtonDown)
{
ListBox lb = sender as ListBox;
if (lb.SelectedItem != null)
{
lb.DoDragDrop(lb, DragDropEffects.Move);
}
}
} private void listBox_MouseUp(object sender, MouseEventArgs e)
{
isLeftMouseButtonDown = false;
}试试这个 这个我试过的 可以的 两个listbox都注册一下
第二步: 将窗体、ListBox1、ListBox2的 AllowDrop
第三步:在ListBox1的MouseDown事件里面放代码:int iSelectedIndex = this.listBox1.SelectedIndex;
listBox1.DoDragDrop(listBox1.Items[iSelectedIndex], DragDropEffects.Move);
第四步:在ListBox2的DragEnter事件里面放代码:if (e.Data.GetDataPresent("Text"))
{
e.Effect= DragDropEffects.Move;
}第五步:在ListBox2的DragDrop事件里放代码listBox2.Items.Add(e.Data.GetData("Text"));
listBox1.Items.Remove(e.Data.GetData("Text"));
另外你需要写点代码在ListBox1里面添加几个测试数据; 完成后编译运行。
第三步的第二行代码: listBox1.DoDragDrop(listBox1.Items[iSelectedIndex], DragDropEffects.Move);
帖子乱了还有问题:QQ: 920851350