c# treeview子节点拖动
treeview里面子节点间的数据拖动(已实现),但是我想在拖动的过程中,把要拖动的数据显示出来(不是弹出框显示)的那种!有哪位大侠会这方面的知识,小弟感激不尽,解决问题...
treeview里面子节点间的数据拖动(已实现),但是我想在拖动的过程中,把要拖动的数据显示出来(不是弹出框显示)的那种!有哪位大侠会这方面的知识,小弟感激不尽,解决问题了一定追分!!!
数据库有studentID和studentname这两个字段,我要在拖动的过程中,显示所拖动的数据(也就是显示拖动的是哪个studentID哪个studentname) 展开
数据库有studentID和studentname这两个字段,我要在拖动的过程中,显示所拖动的数据(也就是显示拖动的是哪个studentID哪个studentname) 展开
3个回答
展开全部
你试一下,新建一个项目,加一个TreeView控件,应该可以运行:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{//添加树形视图子节点
this.treeView1.Nodes.AddRange(new TreeNode[] {
new TreeNode("中国直辖市", new TreeNode[] {
new TreeNode("北京市", new TreeNode[] {
new TreeNode("东城区"), new TreeNode("西城区"),new TreeNode("朝阳区")}),
new TreeNode("上海市", new TreeNode[] {new TreeNode("长宁区"),
new TreeNode("黄埔区"), new TreeNode("浦东区")}),
new TreeNode("重庆市", new TreeNode[] {
new TreeNode("长寿区", new TreeNode[] {new TreeNode("云台镇"),
new TreeNode("凤城镇", new TreeNode[] )}),
new TreeNode("渝北区"),new TreeNode("北碚区")})})});
this.treeView1.AllowDrop = true;
this.treeView1.ExpandAll();
}
private void treeView1_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
{ //处理 treeView1控件ItemDrag事件
//设置拖放类型为移动
DoDragDrop(e.Item, DragDropEffects.Move);
}
private void treeView1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{//处理 treeView1控件DragEnter事件
//获取节点的数据内容
object MyData = e.Data.GetData(typeof(TreeNode));
//如果节点有数据,拖放目标允许移动
if (MyData != null)
{
e.Effect = DragDropEffects.Move;
}
else
{
e.Effect = DragDropEffects.None;
}
TreeView MyTreeView = (TreeView)sender;
TreeNode MyNode = MyTreeView.GetNodeAt(treeView1.PointToClient(new Point(e.X, e.Y)));
if (MyNode != null)
{ //改变进入节点的背景色
MyNode.BackColor = Color.Blue;
//保存此节点,进入下一个时还原背景色
MyOldNode = MyNode;
}
}
private void treeView1_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
{//处理 treeView1控件DragOver事件
//修改鼠标进入节点的背景色,还原上一个节点的背景色
TreeView MyTreeView = (TreeView)sender;
TreeNode MyNode = MyTreeView.GetNodeAt(treeView1.PointToClient(new Point(e.X, e.Y)));
if ((MyNode != null) && (MyNode != MyOldNode))
{
MyOldNode.BackColor = Color.White;
MyNode.BackColor = Color.Blue;
MyOldNode = MyNode;
}
}
private void treeView1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{//处理 treeView1控件DragDrop事件
TreeNode MyNode = (TreeNode)e.Data.GetData(typeof(TreeNode));
TreeView MyTreeView = (TreeView)sender;
//得到当前鼠标进入的节点
TreeNode MyTargetNode = MyTreeView.GetNodeAt(treeView1.PointToClient(new Point(e.X, e.Y)));
if (MyTargetNode != null)
{
TreeNode MyTargetParent = MyTargetNode.Parent;
//删除拖放的节点
MyNode.Remove();
//添加到目标节点
MyTargetNode.Nodes.Add(MyNode);
MyTargetNode.BackColor = Color.White;
MyTreeView.SelectedNode = MyTargetNode;
}
}
//保存前一个鼠标进入的节点
private TreeNode MyOldNode;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{//添加树形视图子节点
this.treeView1.Nodes.AddRange(new TreeNode[] {
new TreeNode("中国直辖市", new TreeNode[] {
new TreeNode("北京市", new TreeNode[] {
new TreeNode("东城区"), new TreeNode("西城区"),new TreeNode("朝阳区")}),
new TreeNode("上海市", new TreeNode[] {new TreeNode("长宁区"),
new TreeNode("黄埔区"), new TreeNode("浦东区")}),
new TreeNode("重庆市", new TreeNode[] {
new TreeNode("长寿区", new TreeNode[] {new TreeNode("云台镇"),
new TreeNode("凤城镇", new TreeNode[] )}),
new TreeNode("渝北区"),new TreeNode("北碚区")})})});
this.treeView1.AllowDrop = true;
this.treeView1.ExpandAll();
}
private void treeView1_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
{ //处理 treeView1控件ItemDrag事件
//设置拖放类型为移动
DoDragDrop(e.Item, DragDropEffects.Move);
}
private void treeView1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{//处理 treeView1控件DragEnter事件
//获取节点的数据内容
object MyData = e.Data.GetData(typeof(TreeNode));
//如果节点有数据,拖放目标允许移动
if (MyData != null)
{
e.Effect = DragDropEffects.Move;
}
else
{
e.Effect = DragDropEffects.None;
}
TreeView MyTreeView = (TreeView)sender;
TreeNode MyNode = MyTreeView.GetNodeAt(treeView1.PointToClient(new Point(e.X, e.Y)));
if (MyNode != null)
{ //改变进入节点的背景色
MyNode.BackColor = Color.Blue;
//保存此节点,进入下一个时还原背景色
MyOldNode = MyNode;
}
}
private void treeView1_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
{//处理 treeView1控件DragOver事件
//修改鼠标进入节点的背景色,还原上一个节点的背景色
TreeView MyTreeView = (TreeView)sender;
TreeNode MyNode = MyTreeView.GetNodeAt(treeView1.PointToClient(new Point(e.X, e.Y)));
if ((MyNode != null) && (MyNode != MyOldNode))
{
MyOldNode.BackColor = Color.White;
MyNode.BackColor = Color.Blue;
MyOldNode = MyNode;
}
}
private void treeView1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{//处理 treeView1控件DragDrop事件
TreeNode MyNode = (TreeNode)e.Data.GetData(typeof(TreeNode));
TreeView MyTreeView = (TreeView)sender;
//得到当前鼠标进入的节点
TreeNode MyTargetNode = MyTreeView.GetNodeAt(treeView1.PointToClient(new Point(e.X, e.Y)));
if (MyTargetNode != null)
{
TreeNode MyTargetParent = MyTargetNode.Parent;
//删除拖放的节点
MyNode.Remove();
//添加到目标节点
MyTargetNode.Nodes.Add(MyNode);
MyTargetNode.BackColor = Color.White;
MyTreeView.SelectedNode = MyTargetNode;
}
}
//保存前一个鼠标进入的节点
private TreeNode MyOldNode;
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
拖个tooltip一切OK
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你要拿什么显示啊?就自定义一个事件吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询