c# winform TreeView與ListView的項互相拖動的應用


#region 將LISTVIEW項拖動TREEVIEW中

        
/// <summary>
        
/// 對選中項指定Move的“開始拖動操作”
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        private void listView1_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
        {
            
this.DoDragDrop(e.Item,DragDropEffects.Move);
        }

        
private void listView1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
        {
            e.Effect
=DragDropEffects.Move;
        }

        
private void treeView1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
        {
            
//判定是否現在拖動的數據是LISTVIEW項
            ListViewItem lvi = (ListViewItem)e.Data.GetData(typeof(ListViewItem)); 
            
if (lvi != null
            { 
                e.Effect 
= DragDropEffects.Move; 
            } 
            
else 
                Cursor 
= Cursors.No; 

        }
        
private void treeView1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
        {
            
//獲得拖放中的項
            ListViewItem lvi = (ListViewItem)e.Data.GetData(typeof(ListViewItem)); 

            
//根據鼠標坐標確定要移動到的目標節點
            Point pt;
            TreeNode targeNode;
            pt 
= ((TreeView)(sender)).PointToClient(new Point(e.X, e.Y));
            targeNode 
= this.treeView1.GetNodeAt(pt);
            TreeNode NewMoveNode  
=new TreeNode(lvi.Text);
            
if(targeNode!=null)//如果目標節點未選中,則添加到一級節點
            {
                targeNode.Nodes.Add(NewMoveNode);
                
//更新當前拖動的節點選擇
                treeView1.SelectedNode = NewMoveNode;
                
//展開目標節點,便於顯示拖放效果
                targeNode.Expand();
                treeView1.Refresh();
            }
            
else
            {
                
this.treeView1.Nodes.Add(NewMoveNode);
                
//更新當前拖動的節點選擇
                treeView1.SelectedNode = NewMoveNode;
                
//展開目標節點,便於顯示拖放效果
                treeView1.ExpandAll();
                treeView1.Refresh();
            }
            
        }
        
#endregion

 

#region 將treeView的項拖動到ListView

        
//定義被選擇的項
        private TreeNode _selectednode;

        
/// <summary>
        
/// 選擇treeView的項
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        private void treeView2_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            
try 
            {
                _selectednode 
= e.Node; 
            }
            
catch { }
        }
        
/// <summary>
        
/// 對選中項指定Move的“開始拖動操作”
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        private void treeView2_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
        {
            
this.DoDragDrop(_selectednode,DragDropEffects.Move);
        }

        
private void treeView2_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
        {
            e.Effect
=DragDropEffects.Move;
        }

        
/// <summary>
        
/// 添加MouseDown是因為,MouseDown先執行AfterSelect,所以在沒有AfterSelect時,MouseDown要先選中被選取項
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        private void treeView2_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            
try 
            { 
                
if ((e.Button & MouseButtons.Left) == MouseButtons.Left) 
                { 
                    _selectednode 
= treeView2.GetNodeAt(e.X,e.Y); 
                    treeView2.SelectedNode 
= _selectednode;
                } 
            } 
            
catch { } 

        }

        
private void FillListView(TreeNodeCollection nodes) 
        { 
            
for (int i = 0; i < nodes.Count; i++
            { 
                ListViewItem lvwItem 
= new ListViewItem(nodes[i].Text, nodes[i].ImageIndex); 
                lvwItem.Tag 
= nodes[i].Tag; 
                listView2.Items.Add(lvwItem); 
            } 
        }

        
//當選中項移到listView的工作區時,指定被拖動的目標的移動方式
        private void listView2_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
        {
            
try 
            { 
                TreeNode node 
= (TreeNode)e.Data.GetData(typeof(TreeNode)); 
                
if (node != null
                { 
                    e.Effect 
= DragDropEffects.Move; 
                } 
                
else 
                    Cursor 
= Cursors.No; 
            } 
            
catch { } 
            
finally { Cursor = Cursors.Default; } 

        }

        
//當選中項在listView在隨SHU標移動時,指定被拖動的目標的移動方式
        private void listView2_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            
try 
            { 
                
if (_selectednode != null & (e.Button & MouseButtons.Left) == MouseButtons.Left) 
                { 
                    DragDropEffects dropEffect 
= listView2.DoDragDrop(_selectednode, DragDropEffects.Move); 
                } 
            } 
            
catch { } 

        }

        
//當被放入控件時
        private void listView2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
        {
            
try 
            { 
                TreeNode node 
= (TreeNode)e.Data.GetData(typeof(TreeNode)); 
                
if (node != null
                    listView2.Items.Add(node.Text, node.ImageIndex); 
            } 
            
catch { } 
            
finally { Cursor = Cursors.Default; } 
        }

        
#endregion


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com