ListView 拖拽实例

实例内容:
ListView之间单项数据拖拽;
ListView之间多项数据拖拽;
ListView之间多项数据双向拖拽;
ListView内部位置移动;
ListView内部值的交换。

部分代码:

        #region ListView之间单项数据拖拽
        
private void listView4_ItemDrag(object sender, ItemDragEventArgs e)
        {
            listView4.DoDragDrop(e.Item, DragDropEffects.Move);
        }

        
private void listView5_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect 
= e.AllowedEffect;
        }

        
private void listView5_DragDrop(object sender, DragEventArgs e)
        {
            ListViewItem draggedItem 
= (ListViewItem)e.Data.GetData(typeof(ListViewItem));
            listView5.Items.Insert(
0,(ListViewItem)draggedItem.Clone());
        }
        
#endregion
        #region ListView之间多项数据拖拽
        
private void listView2_ItemDrag(object sender, ItemDragEventArgs e)
        {
            ListViewItem[] items 
= new ListViewItem[((ListView)(sender)).SelectedItems.Count];
            
int i = 0;

            
foreach (ListViewItem item in ((ListView)(sender)).SelectedItems)
            {
                items[i] 
= item;
                i
++;
            }

            ((ListView)(sender)).DoDragDrop(
new DataObject("System.Windows.Forms.ListViewItem()", items), DragDropEffects.Copy);
        }

        
private void listView3_DragEnter(object sender, DragEventArgs e)
        {
            
if (e.Data.GetDataPresent("System.Windows.Forms.ListViewItem()"))
            {
                e.Effect 
= DragDropEffects.Copy;
            }
            
else
            {
                e.Effect 
= DragDropEffects.None;
            }
        }   

        
private void listView3_DragDrop(object sender, DragEventArgs e)
        {
            ListViewItem[] items 
= (ListViewItem[])(e.Data.GetData("System.Windows.Forms.ListViewItem()"));
            
int i = 0;

            
foreach (ListViewItem item in items)
            {
                ListViewItem listItem 
= new ListViewItem(items[i].Text,item.ImageIndex);
                listItem.SubItems.Add(items[i].SubItems[
1].Text);
                listItem.SubItems.Add(items[i].SubItems[
2].Text);
                ((ListView)(sender)).Items.Add(listItem);
                
try
                {
                    
if (sender == listView3)
                    {
                       listView2.Items.Remove(listView2.SelectedItems[
0]);                       
                    }
                }
                
catch (Exception exc)
                {
                    MessageBox.Show(exc.Message);
                }
                
                i
++;
            }
        }
        
#endregion
        #region  ListView内部位置移动
        
private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
        {
            listView1.DoDragDrop(e.Item, DragDropEffects.Move);
        }

        
private void listView1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect 
= e.AllowedEffect;
        }

        
private void listView1_DragOver(object sender, DragEventArgs e)
        {
            Point targetPoint 
= listView1.PointToClient(new Point(e.X, e.Y));
            
int targetIndex = listView1.InsertionMark.NearestIndex(targetPoint);

            
if (targetIndex>-1)
            {
                Rectangle itemBounds 
= listView1.GetItemRect(targetIndex);
                
if (targetPoint.X>itemBounds.Left+(itemBounds.Width/2))
                {
                    listView1.InsertionMark.AppearsAfterItem 
= true;
                }
                
else
                {
                    listView1.InsertionMark.AppearsAfterItem 
= false;
                }
            }
            listView1.InsertionMark.Index 
= targetIndex;
        }

        
private void listView1_DragLeave(object sender, EventArgs e)
        {
            listView1.InsertionMark.Index 
= -1;
        }

        
private void listView1_DragDrop(object sender, DragEventArgs e)
        {
            
int targetIndex = listView1.InsertionMark.Index;
            
if (targetIndex==-1)
            {
                
return;
            }

            
if (listView1.InsertionMark.AppearsAfterItem)
            {
                targetIndex
++;
            }

            ListViewItem draggedItme 
= (ListViewItem)e.Data.GetData(typeof(ListViewItem));

            listView1.Items.Insert(targetIndex,(ListViewItem)draggedItme.Clone());
            listView1.Items.Remove(draggedItme);
        }

        
private class ListViewIndexComparer : System.Collections.IComparer
        {
            
public int Compare(object x, object y)
            {
                
return ((ListViewItem)x).Index - ((ListViewItem)y).Index;
            }
        }
        
#endregion        
。。。。。。
下载:https://files.cnblogs.com/EasyData/ListViewDragTest.rar
posted on 2008-03-30 18:36  迷你软件  阅读(3429)  评论(4编辑  收藏  举报

本网站绝大部分资源来源于Internet,本站所有作品版权归原创作者所有!!如有以下内容:章节错误、非法内容、作者署名出错、版权疑问、作品内容有违相关法律等请及时与我联系. 我将在第一时间做出响应!本站所有文章观点不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。