c# winform TreeView与ListView的项互相拖动的应用[转载]
轉載modede
很久沒寫教程了,停頓了有兩年之久了。今天我們來討論討論 winform 中TreeView與ListView,通觀兩者很相似,只是一個是樹形結構,一個是列表結構。
今天自己創建的群里(2175832)有個朋友問到ListView的項如何拖動到TreeView中,并生成子節點,簽于麻煩,我回答比較麻煩,用BUTTON實現吧,但他覺得拖動比較方便,那當然哦!無奈幫人幫到底,送佛送到西,于是立即創建本地WINFORM 測試項目,給了點代碼給他(其實他本身已經完成了一半代碼量了,只是在ListView拖動項到控區域外遇到了不解之疑)
好了,廢話不多說了,直接轉正題吧。今天我帖了代碼是兩個功能的結合:
1、ListView項拖動到TreeView
2、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
http://blog.csdn.net/modede/archive/2008/05/27/2486663.aspx
總結
以上是生活随笔為你收集整理的c# winform TreeView与ListView的项互相拖动的应用[转载]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Silverlight 5 Beta新特
- 下一篇: Google真相:决策贡献及其艰苦抉择