C#中如何进行两个listbox之间的拖动
1个回答
展开全部
假设有listBox1, listBox2。
假设从listBox1拖到listBox2:
首先要AllowDrop:listBox2.AllowDrop = true;
然后listBox1响应mousedown事件:
private void listBox1_MouseDown(
object sender, System.Windows.Forms.MouseEventArgs e)
{
if(listBox1.Items.Count==0)
return;
int index = listBox1.IndexFromPoint(e.X,e.Y);
string s = listBox1.Items[index].ToString();
DragDropEffects dde1=DoDragDrop(s,
DragDropEffects.All);
if(dde1 == DragDropEffects.All )
{
listBox1.Items.RemoveAt(listBox1.IndexFromPoint(e.X,e.Y));
}
}
当拖动到listBox2时,响应DragDorp:
private void listBox2_DragOver(
object sender, System.Windows.Forms.DragEventArgs e)
{
e.Effect=DragDropEffects.All;
}
最后drop事件:
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);
}
}
假设从listBox1拖到listBox2:
首先要AllowDrop:listBox2.AllowDrop = true;
然后listBox1响应mousedown事件:
private void listBox1_MouseDown(
object sender, System.Windows.Forms.MouseEventArgs e)
{
if(listBox1.Items.Count==0)
return;
int index = listBox1.IndexFromPoint(e.X,e.Y);
string s = listBox1.Items[index].ToString();
DragDropEffects dde1=DoDragDrop(s,
DragDropEffects.All);
if(dde1 == DragDropEffects.All )
{
listBox1.Items.RemoveAt(listBox1.IndexFromPoint(e.X,e.Y));
}
}
当拖动到listBox2时,响应DragDorp:
private void listBox2_DragOver(
object sender, System.Windows.Forms.DragEventArgs e)
{
e.Effect=DragDropEffects.All;
}
最后drop事件:
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);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询