转载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