这个文章与之前写的“C#TreeView窗体版点击右键弹出菜单+右键获取信息”,有些不一样的地方。改进了,右键“新建”可以直接添加新的节点并且在节点上直接修改名字;右键“重命名”可以直接在节点上修改节点名称。
下载资源 http://download.csdn.net/detail/small5e4444/3891551
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace treeview2
{
public partial class Form1 : Form
{
dbhelper db;
string cao = "";
TreeNode tnd;
string Nname = "";//传值 节点的名字
string Ntag = "";//传值 节点tag
int isQK = 0;//判断是添加 修改 还是删除
TreeView treev;//右键选择的项
string oldName = "";//修改前的节点名称
TreeNode newTN;//新建的 节点
public Form1()
{
db = new dbhelper();
InitializeComponent();
bdtree();
//重命名ToolStripMenuItem.Enabled = false;//让目录中的按钮 失效
}
private void bdtree()
{
db.Execute("update t_tree set isbd=0", CommandType.Text);
DataTable dt;//
TreeNode tn;//根目录
TreeNode tn2;//子目录
while (1 == 1)
{
dt = db.GetDateTable("select * from t_tree where isbd=0 order by gxid");
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)//循环剩下的数据
{
if (dt.Rows[i][2].ToString() == "0")//根目录
{
tn = new TreeNode();
tn.Text = dt.Rows[i][1].ToString();
tn.Tag = dt.Rows[i][0].ToString();
treeView1.Nodes.Add(tn);
db.Execute("update t_tree set isbd=1 where id=" + dt.Rows[i][0].ToString() + "", CommandType.Text);
//dt1 = db.GetDateTable("select * from t_tree where gxid=" + dt.Rows[i][0].ToString() + "");
}
else//非根目录
{
foreach (TreeNode ts in treeView1.Nodes)
{
TreeNode targetNode = SearchNode(ts, dt.Rows[i][2].ToString());
if (targetNode != null)
{
tn2 = new TreeNode();
tn2.Text = dt.Rows[i][1].ToString();
tn2.Tag = dt.Rows[i][0].ToString();
targetNode.Nodes.Add(tn2);
db.Execute("update t_tree set isbd=1 where id=" + dt.Rows[i][0].ToString() + "", CommandType.Text);
break;
}
}
}
}
}
else
{
break;
}
}
}
private TreeNode SearchNode(TreeNode td, string selectParentNum)//递归
{
if (td.Tag.ToString() == selectParentNum)
{
return td;
}
TreeNode targetNode = null;
foreach (TreeNode childNodes in td.Nodes)
{
targetNode = SearchNode(childNodes, selectParentNum);
if (targetNode != null)
break;
}
return targetNode;
}
private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
{
cao = e.Item.ToString();
cao = cao.Substring(10);//获取选中的节点名字
tnd = (TreeNode)e.Item;
DataTable dt2 = db.GetDateTable("select * from t_tree where name='" + cao + "'");
//DataTable dt2 = db.GetDateTable("select * from t_tree where gxid='" + dt1.Rows[0][0].ToString() + "'");
//string cc = dt2.Rows[0][1].ToString();
this.DoDragDrop(e.Item, DragDropEffects.Move);
}
private void treeView1_DragDrop(object sender, DragEventArgs e)
{
//获得拖放中的项
// ListViewItem lvi = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
//TreeView tr = (TreeView)e.Data.GetData(typeof(TreeView));// e.Data.ToString();
//string a=tr.Nodes.ToString();
//string cao =e.Data.ToString();//;;.Data.ToString();
//根据鼠标坐标确定要移动到的目标节点
Point pt;
TreeNode targeNode;
pt = ((TreeView)(sender)).PointToClient(new Point(e.X, e.Y));
targeNode = this.treeView1.GetNodeAt(pt);
TreeNode NewMoveNode = new TreeNode(cao);
if (targeNode != null)//如果目标节点未选中,则添加到一级节点
{
string mb = targeNode.Text;
DataTable dt = db.GetDateTable("select * from t_tree where name='" + mb + "'");
db.Execute("update t_tree set gxid=" + dt.Rows[0][0].ToString() + " where name='" + cao + "'", CommandType.Text);
treeView1.Nodes.Clear();
bdtree();//重调排序方法
//targeNode.Nodes.Add(NewMoveNode);
////更新当前拖动的节点选择
//treeView2.SelectedNode = NewMoveNode;
//展开目标节点,便于显示拖放效果
targeNode.Expand();
treeView1.ExpandAll();
treeView1.Refresh();
}
else
{
db.Execute("update t_tree set gxid=0 where name='" + cao + "'", CommandType.Text);
treeView1.Nodes.Clear();
bdtree();//重调排序方法
//this.treeView2.Nodes.Add(NewMoveNode);
////更新当前拖动的节点选择
//treeView2.SelectedNode = NewMoveNode;
//展开目标节点,便于显示拖放效果
treeView1.ExpandAll();
treeView1.Refresh();
}
}
private void treeView1_DragEnter(object sender, DragEventArgs e)
{
//判定是否现在拖动的数据是LISTVIEW项
//ListViewItem lvi = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
string lvi = e.Data.ToString();
if (lvi != null)
{
e.Effect = DragDropEffects.Move;
treeView1.Nodes.Remove(tnd);
}
else
Cursor = Cursors.No;
}
private void treeView1_MouseUp(object sender, MouseEventArgs e)
{
treev = sender as TreeView;
Point point = treev.PointToClient(Cursor.Position);
TreeViewHitTestInfo info = treev.HitTest(point.X, point.Y);
TreeNode node = info.Node;//获得 右键点击的节点
if (node != null && MouseButtons.Right == e.Button)
{
treev.SelectedNode = node;//关键的一句话,右键点击菜单的时候,会选中右键点击的项
Nname = node.Text;
oldName = node.Text;
Ntag = node.Tag.ToString();
}
}
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)//点击 目录的事件
{
ToolStripMenuItem tsmi = sender as ToolStripMenuItem;
if (tsmi.Text == "重命名")
{
treev.SelectedNode.BeginEdit();
isQK = 1;
}
if (tsmi.Text == "新建")
{
newTN = new TreeNode();
newTN.Text = "新建文件夹";
treev.SelectedNode.Nodes.Add(newTN);
// treeView1.Nodes.Add(tn);
newTN.BeginEdit();//编辑节点的方法
isQK = 2;
}
}
private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)//编辑节点 结束时发生的时间
{
string newNodeName = e.Label;
string Ntag = treev.SelectedNode.Tag.ToString();//获得 右键选中的tag
if (newNodeName != null && isQK == 1)//重命名
{
db.Execute("update t_tree set name='" + newNodeName + "' where name='" + oldName + "'", CommandType.Text);
}
else if (isQK == 2)//新建
{
if (newNodeName == null)//如果新建文件 没有修改名称
{
newNodeName = "新建文件夹";
}
DataTable dt = db.GetDateTable("select * from t_tree where name='" + newNodeName + "'");
if (dt.Rows.Count > 0)//新建文件夹的名字 数据库中已经有了
{
newTN.BeginEdit();
}
else
{
db.Execute("insert into t_tree values('" + newNodeName + "'," + Ntag + ",1)", CommandType.Text);
dt = db.GetDateTable("select id from t_tree where name='" + newNodeName + "'");//获得 新节点的id 然后负值给树
newTN.Tag = dt.Rows[0][0].ToString();
}
}
}
}
}