PDA

View Full Version : cần giúp về kỹ thuật drap drop



zKeitherz
09-11-2010, 00:59
chào mọi người!
mình có 1 form có 2 listview lv1 lv2

mình có đoạn code sau đây chạy thì ko lỗi nhưng drap item từ lv1 qua lv2 ko dc


private void lv1_MouseMove(object sender, MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left && lv1.SelectedItems.Count > 0)
{
lv1.DoDragDrop(lv1.SelectedItems, DragDropEffects.Move);
}
}

private void lv2_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(System.String)))
{
if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move)
{
e.Effect = DragDropEffects.Move;
}
}
}

private void lv2_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(System.String)))
{
Object item = (object)e.Data.GetData(typeof(System.String));
if (e.Effect == DragDropEffects.Move)
{
lv2.Items.Add("Aa");
}
}
}

private void lv1_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
e.UseDefaultCursors = false;
Cursor.Current = Cursors.Hand;
}


mình debug thử thì thấy hàm lv2_DragDrop ko bao giờ dc kích hoạt cả
nên có thể vì như vậy mà ko add dc item vào lv2

mong mọi người chỉ giúp
xin cám ơn

anhchanghaudau
09-11-2010, 02:01
chào mọi người!
mình có 1 form có 2 listview lv1 lv2

mình có đoạn code sau đây chạy thì ko lỗi nhưng drap item từ lv1 qua lv2 ko dc


private void lv1_MouseMove(object sender, MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left && lv1.SelectedItems.Count > 0)
{
lv1.DoDragDrop(lv1.SelectedItems, DragDropEffects.Move);
}
}

private void lv2_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(System.String)))
{
if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move)
{
e.Effect = DragDropEffects.Move;
}
}
}

private void lv2_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(System.String)))
{
Object item = (object)e.Data.GetData(typeof(System.String));
if (e.Effect == DragDropEffects.Move)
{
lv2.Items.Add("Aa");
}
}
}

private void lv1_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
e.UseDefaultCursors = false;
Cursor.Current = Cursors.Hand;
}


mình debug thử thì thấy hàm lv2_DragDrop ko bao giờ dc kích hoạt cả
nên có thể vì như vậy mà ko add dc item vào lv2

mong mọi người chỉ giúp
xin cám ơn
Định thuộc tính AllowDrop = true thì nó mới tiếp nhận sự kiện drop

zKeitherz
09-11-2010, 11:00
mình làm dc rồi
nhưng mà ko biết làm sao để khi drap item ra ngoài vùng lv2 thì con chuột nó sẽ hiện dấu cấm nhĩ ?

thanks

hoatc
10-11-2010, 11:28
mình làm dc rồi
nhưng mà ko biết làm sao để khi drap item ra ngoài vùng lv2 thì con chuột nó sẽ hiện dấu cấm nhĩ ?

thanks

---------

Bạn sử dụng Cursor = Cursors.No nhé (tại vùng muốn nó bị cấm ấy)

zKeitherz
11-11-2010, 12:25
mình thêm hàm nào mà cũng ko tác dụng
private void lv2_DragLeave(object sender, EventArgs e)
{
Cursor.Current = Cursors.Arrow;
}

à thôi mình hiểu rồi
tại cái hàm lv1_GiveFeedback, bỏ nó đi là ok